Partial View List returns null when saving the model

a 夏天 提交于 2019-12-13 07:49:26

问题


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?


回答1:


Your current code of partial view has only the Call to DisplayNameFor helper method which will only render the display name as a label. If you want to submit the data of each item, you need to generate input form fields with matching names with your view model property structure.

Assuming your StudentVM has a Students collection of type IEnumerable<Students>

If your HttpPost action method's parameter is of type StudentVm, you need to make sure that your partial view is generating the input form fields with name like 'Students[0].Name, 'Students[1].Name etc. You can use the Html.TextBox helper method and specify this custom names

@model IEnumerable<Students>
<table class="table-bordered">
    @{
        var counter = 0;
    }
    @foreach (var student in Model)
    {
        <tr>
            <th>Name</th>
            <td>@Html.TextBox("Students[" + counter + "].Name", student.Name)</td>
        </tr>
        counter++;
    }
</table>

If you are simply displaying the names of existing students, you do not need the text fields, you can simply display those inside the loop. In that case, when form submits,why do you worry about the Students collection being null ? You are trying to save a new Student which will be in .Name property. So if you need the existing students again (but why ? ), you can call the GetAllStudents method.




回答2:


maybe you used 2 Different model in view ...... you can use 2 model in 1 viewmodel It is not necessary partial view

ViewModels.StudentVM != IEnumerable<Entities.Students>

you can all data (many model) passed in 1 view model :

var vm = new TestViewModel();
vm.one = db.one.tolist();
vm.two = db.two.tolist();


来源:https://stackoverflow.com/questions/39194371/partial-view-list-returns-null-when-saving-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!