gpt4 book ai didi

c# - mvc中的批量更新

转载 作者:行者123 更新时间:2023-11-30 16:55:36 26 4
gpt4 key购买 nike

我想更新用户列表,在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com