c# - mvc中的批量更新

标签 c# asp.net-mvc

我想更新用户列表,在我的 Controller 中我已经返回了用户列表

return View("usersList", user);

在 View 中我使用

@using (Html.BeginForm("usersList", "UserManagement"))
{
    @Html.EditorFor(model => model, "tbl_UserList");
}

在编辑器模板中,我使用如下“tbl_UserList.cshtml”

@model IList<tbl_Users>

@using (Html.BeginForm())
{
<table>
    <tr>
    <th>
        @Html.DisplayNameFor(model => model[0].FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model[0].LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model[0].Email)
    </th>

    </tr>

@foreach (var item in Model) {
    <tr>
    <td>
        @Html.EditorFor(modelitem => item.FirstName)
    </td>
    <td>
        @Html.EditorFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.EditorFor(modelItem => item.Email)
    </td>
    </tr>
}

</table>

<input id="subButton" type="submit" value="submit" title="submit data" />
}

在 Controller 中

[HttpPost]
public ActionResult UserList(List<tbl_Users> users)
{
    //..
}

这里controller里面的users是null,怎么解决> 谢谢

最佳答案

将您的 EditorTemplate 更改为

@model tbl_Users
<tr>
  <td>@Html.EditorFor(m => m.FirstName)</td>
  <td>@Html.EditorFor(m => m.LastName)</td>
  <td>@Html.EditorFor(m => m.Email)</td>
</tr>

并将其重命名为tbl_Users.cshtml

然后在主视图

@model IList<tbl_Users>
@using (Html.BeginForm("usersList", "UserManagement"))
{
  <table>
    <thead>
      <tr>
        <td>@Html.DisplayNameFor(m => m.FirstName)</td>
        ....
      </tr>
    </thead>
    <tbody>
      @Html.EditorFor(m => m); // do not specify a name
    </tbody>
  </table>
}

请注意,您当前的 foreach 循环正在生成重复的 id 属性(无效的 html)和重复的 name 属性,它们不会绑定(bind)到集合(检查前后的 html 以了解)。

关于c# - mvc中的批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296217/

相关文章:

c# - 无法加载文件或程序集“System.Security.Cryptography.Algorithms,版本 = 4.1.0.0

javascript - 为什么此 Code Magazine 示例在 Chrome 中(仅)失败?

c# - 如何找到给定字符串中最长的回文?

c# - 使用 Json.NET 反序列化带有数组数组的 json 字符串

c# - 如何将 DateTimePicker 转换为日期以保存到 SQL Server?

asp.net-mvc - 领域驱动的编程和事件

asp.net-mvc - 在哪里以及如何验证和映射 ViewModel?

c# - 为什么我的 Azure 中托管的 ASP NET Web 服务没有执行任何操作?

asp.net-mvc - 在没有 Visual Studio 的情况下获取 SQL Server 的连接字符串

c# - 将非托管 dll 嵌入托管 C# dll