保存模型时,局部视图列表返回null [英] Partial View List returns null when saving the model

查看:80
本文介绍了保存模型时,局部视图列表返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将List传递给局部视图,它可以正常工作,可以显示所有数据,但是当我保存模型时,List返回null,我会丢失什么?

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?

  • 不注意对象,我为示例编写了假对象.

这是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.

局部视图:

@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>

我想让所有选中的学生,所以可以说我将选择3个学生.然后点击保存按钮.结果:尽管我选择了3个学生,但Model.Students为null.我怎样才能得到那些学生?

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?

推荐答案

您当前的局部视图代码仅具有对 DisplayNameFor 助手方法的调用,该方法只会将显示名称呈现为标签.如果要提交每个项目的数据,则需要使用视图模型属性结构生成名称匹配的输入表单字段.

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.

假定您的 StudentVM 具有类型为 IEnumerable< Students>

如果HttpPost操作方法的参数的类型为 StudentVm ,则需要确保部分视图正在生成名称为'Students [0] .Name 之类的输入表单字段,'学生[1].名称等.您可以使用Html.TextBox帮助器方法并指定此自定义名称

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>

如果仅显示现有学生的姓名,则不需要文本字段,只需在循环中显示这些内容即可.在这种情况下,提交表单时,您为什么担心Student集合为 null ?您正在尝试保存一个新的Student,该学生将位于 .Name 属性中.因此,如果您再次需要现有的学生(但为什么要这样做?),则可以调用 GetAllStudents 方法.

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.

这篇关于保存模型时,局部视图列表返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆