ASP.NET MVC的复杂模型修正 [英] ASP.NET MVC Complex Model Updating

查看:115
本文介绍了ASP.NET MVC的复杂模型修正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个列表℃的PersonViewModel; SubViewModel> (姓名,地址等)

I have a PersonViewModel that consists of several List<SubViewModel> (Name, Address, etc.)

我有一个HTML表单为每个SubViewModel,在这里用户可以更新,添加,删除,设置的项目,为个人的初级入门表。

I have an Html Form with tables for each SubViewModel, where the user can update,add,delete and set items to the primary entry for the person.

有关名称的简化表:

@foreach (PersonNameViewModel name in Model.Names)
{
  <tr>
     <td>
         @Html.DisplayFor(modelItem => name.FullName)
     </td>
     <td>
         @Html.DisplayFor(modelItem => name.FirstName)
     </td>
     <td>
         @Html.DisplayFor(modelItem => name.MiddleName)
     </td>
     <td>
         @Html.DisplayFor(modelItem => name.LastName)
     </td>
     <td class="center">
         @Html.RadioButtonFor(modelItem => name.IsPrimary, "")
     </td>
     <td class="center">
         @Html.CheckBoxFor(modelItem => name.ToDelete)
     </td>
  </tr>
}

从这里,我不知道如何让所有的数据回PersonViewModel当用户提交表单,更新个人记录。如果我只是在我的岗位控制器接受 PersonViewModel ,它是完全空的。我看着的FormCollection,但我不知道这该是正确的。有经验的人能指出我在为构建我的模型备份更新正确的方向?三江源非常多。

From here, I don't know how to get all the data back into the PersonViewModel when the user submits the form, to update the person record. If I simply accept a PersonViewModel in my POST controller, it's completely empty. I looked at FormCollection, but I'm not sure if that's right for this. Can someone with experience point me in the right direction for building my model back up for updating? Thankyou very much.

推荐答案

每个人的答案是正确的,但如果你想更新人们的信息,你需要使用的HTML帮助编辑不是显示。此外,您仍然可以使用的foreach 在你看来,只要改变它象下面这样:

Everyone's answer is correct, But if you want to update people's information, you need to use Html Helper Editor NOT Display. Also, you still can use foreach in your view, just change it like below:

@foreach (var name in Model.Names.Select((value, i) => new { i, value }))
{
    <tr>
        <td>
            @Html.EditorFor(m => m.Names[@name.i].FullName)
        </td>
        <td>
            @Html.EditorFor(m => m.Names[@name.i].FirstName)
        </td>
        <td>
            @Html.EditorFor(m => m.Names[@name.i].MiddleName)
        </td>
        <td>
           @Html.EditorFor(m => m.Names[@name.i].LastName)
        </td>
        <td class="center">
            @Html.RadioButtonFor(m => m.Names[@name.i].IsPrimary,"")
        </td>
        <td class="center">
            @Html.CheckBoxFor(m => m.Names[@name.i].ToDelete)
        </td>
    </tr>
}

这篇关于ASP.NET MVC的复杂模型修正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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