Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

c# - Partial View List returns null when saving the model

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...