I have a viewmodel consisting 2 lists:
public class RandomViewmodel
{
public List<Model1> Model1 { get; set; }
public List<Model2> Model2 { get; set; }
}
When I access these viewmodel to create view that works fine
@for (int i = 0; i < Model.Model1.Count(); i++)
{
x = i;
var item = Model.Model1.ElementAt(i);
<tr>
<td>
@Html.Editor("[" + i + "].Amount", new { htmlAttributes = new { @class = "form-control", @Value = item.Amount } })
</td>
</tr>
}
@for (int k=0; k < Model.Model2.Count; k++)
{
x = x + 1;
var bacc = Model.Model2.ElementAt(k);
@Html.Hidden("[" + k + "].ID", bacc.ID, new { htmlAttributes = new { @id = "[" + k + "].ID" } })
<tr style="font-weight:bold;">
<td style="padding-left: 25px;">
@(x+1) @bacc.name
</td>
<td class="adder_table_right" width="130px">
@Html.Editor("[" + k + "].MiscAmount1", new { htmlAttributes = new { @class = "form-control" } })
</td>
<td class="adder_table_right" width="130px">
@Html.Editor("[" + k + "].MiscAmount2", new { htmlAttributes = new { @class = "form-control" } })
</td>
</tr>
}
Here is my controller action:
[HttpPost]
public ActionResult Index(RandomViewModel vm)
Now when I hit submit button, I can see that the viewmodel object vm
receives NULL for both of the models.