I’m having problems getting the new data from the RandomNumbers List
I got this simple class
public class Test
{
public string Name { get; set; }
public List<int> RandomNumbers { get; set; }
public Test()
{
RandomNumbers = new List<int>(){0,0,0,0,0};
}
}
In my controller I have created two ActionResults one for Get and one for Post
public ActionResult Test()
{
var test = new Test();
return View(test);
}
[HttpPost]
public ActionResult Test(Test test)
{
return View();
}
And Finally the View
@model Test
@{
ViewBag.Title = "Test";
Layout = null;
}
<h2>Test</h2>
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
<ul>@foreach (var t in Model.RandomNumbers)
{
<li>
@Html.LabelFor(model => t)
@Html.EditorFor(model => t)
</li>
}
</ul>
<input type="submit" value="Save"/>
}


The Page looks like this.
Now when I’m clicking the Save button, the [HttpPost] Test is called, but the included Test Object only contains the new Name, none of the Changes from the RandomNumber list is included. What I’m doing wrong?
You have to make sure your nested collection gets rendered in the view accordingly.
In the view:
This will create indexed input fields like
<input id="RandomNumbers_3_" name="RandomNumbers[3]" type="text" value="0" />(skipped validation for brevity)