I have a strongly-typed view, with a list of custom objects in the model.
In the view I display textboxes for every object in the list :
@using (Html.BeginForm("SaveData", "Localization", FormMethod.Post))
{
foreach (YB.LocalizationGlobalText m in Model.GlobalTexts)
{
@Html.Label(m.LocalizationGlobal.Name)
@Html.TextBoxFor(model => m.Text)
<br />
}
<input type="submit" value="Save" />
}
Now how would I get the updated data from the textboxes in my model.
I can see in the formcollection the updated data is there:
[HttpPost]
public virtual ActionResult SaveData(FormCollection form)
{
// Get movie to update
return View();
}
form[“m.Text”] = “testnewdata1,testnewdata”
But how do I get this mapped to the model, so I have the updated values for each object.
Or how can I get it cleanly from the formcollection, something like this .. form[someid][“m.Text”]
Edit:
I also tried passing the model as a parameter, but the model data is empty.
[HttpPost]
public virtual ActionResult SaveData(LocalizationModel model, FormCollection form)
{
// Get movie to update
return View();
}
When I look into the model: model.GlobalTexts = null
Edit See this question I asked a while back: How to use multiple form elements in ASP.NET MVC
Lets say you have a view like this:
Refactor it like this:
Now in your action method do this:
It’s even easier than that if you use editors, but I’ll let you figure those out for yourself as they are super simple. The accepted answer in the question I linked shows an example.
NOTE: you can substitute IList for IEnumerable if you need to.