Post Html table 由 MVC 视图中的模型创建到控制器
Post Html table created by a model in MVC view to controller
我根据脚手架创建了一个列表
@model IEnumerable<FleetLink.Domain.Entities.UserTable>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Master_IP)
@Html.TextBoxFor(modelItem=>item.Master_IP)
</td>
<td>
@Html.DisplayFor(modelItem => item.Master_Name)
</td>
</tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
}
在我的控制器中,我有一个 get 和一个 post 索引方法
[HttpPost]
public ActionResult Index(List<UserTable> list)
{
return View();
}
[HttpGet]
public ActionResult Index()
{
var users= from userTable in _repo.GetUsers()
select userTable;
return View(users);
}
我原以为它会在我单击提交时调用 post 方法,并将整个 tables 数据传递给 Index HTTPPost 方法。但是一直在调用Index的get方法。目标是在用户编辑后传递整个 table 数据,这样我就可以一次保存所有 table 数据。请指教我做错了什么。
我通过将 foreach 更改为 for(int i=0....) 以及更改 @using (Html.BeginForm("Index", "Home" 解决了这个问题, FormMethod.Post)) 到
@using (Html.BeginForm(FormMethod.Post))
谢谢
我根据脚手架创建了一个列表
@model IEnumerable<FleetLink.Domain.Entities.UserTable>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Master_IP)
@Html.TextBoxFor(modelItem=>item.Master_IP)
</td>
<td>
@Html.DisplayFor(modelItem => item.Master_Name)
</td>
</tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
}
在我的控制器中,我有一个 get 和一个 post 索引方法
[HttpPost]
public ActionResult Index(List<UserTable> list)
{
return View();
}
[HttpGet]
public ActionResult Index()
{
var users= from userTable in _repo.GetUsers()
select userTable;
return View(users);
}
我原以为它会在我单击提交时调用 post 方法,并将整个 tables 数据传递给 Index HTTPPost 方法。但是一直在调用Index的get方法。目标是在用户编辑后传递整个 table 数据,这样我就可以一次保存所有 table 数据。请指教我做错了什么。
我通过将 foreach 更改为 for(int i=0....) 以及更改 @using (Html.BeginForm("Index", "Home" 解决了这个问题, FormMethod.Post)) 到 @using (Html.BeginForm(FormMethod.Post))
谢谢