I pass a List to a partial view and it works fine, it shows all the data but when I save the Model, the List returns null, what am I missing?
- Dont pay attention to the objects, I wrote fake ones for the example.
This the cshtml:
@model ViewModels.StudentVM
@using (Html.BeginForm("SaveStudent", "StudentsView", FormMethod.Post}))
{
@Html.AntiForgeryToken();
<div class="row">
<span>Student name:</span>
@Html.TextBoxFor(s => s.Name)
</div>
<div>
@Html.Partial("StudentsList", Model.Students)
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn">
</div>
}
When loading the view I get all the students to the View Model:
vm.Students = await _studentController.GetAllStudents(); // returned 20 Students.
The partial view:
@model IEnumerable<Entities.Students>
<table class="table-bordered">
@foreach (var item in Model)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.IsSelected)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
I would like to get all selected students, so lets say I will select 3 students.
And then click on the save button. Result: the Model.Students is null although it I selected 3 students. How can I get those students?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…