通过索引页面编辑完整的对象列表
Editing full objectlist through Index page
我希望能够在我的索引视图中按下提交按钮,这样可以保存通过我使用的 EditorFors 对列表所做的所有更改。
在编辑视图中,我可以一次编辑和保存一个列表项,但我希望能够一次单击保存完整列表。
我试过用 @using(Html.BeginForm()) {}
在索引视图中包装整个 table,然后在控制器中添加一个 HttpPost 方法,如下所示:
[HttpPost]
public ActionResult Index(List<TileModel> modelList) {
return View(modelList);
}
但是进入这个方法后modelList
为空。我试过将 [Bind("MyModel")]
添加到参数列表,但我对绑定什么有点无能为力..
我基本上是在尝试复制以下功能:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Employment")] TileModel mModel) {
if(ModelState.IsValid) {
db.Entry(mModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mModel);
}
,但让它在索引视图中保存完整列表。
知道如何实现这一目标吗?我是否走在正确的轨道上?
编辑:
这是我的索引视图(我的编辑视图与 visual studio 生成的视图基本没有变化,所以我认为展示它没有那么有趣?)。
@model IEnumerable<Anslagstavlan.Models.TileModel>
@{ ViewBag.Title = "title"; }
<ul class="nav navbar-nav">
<li>@Html.ActionLink("TILES", "tileboard", "tilemodels")</li>
<li>@Html.ActionLink("NEW ENTRY", "Create")</li>
</ul>
@using(Html.BeginForm()) {
<table class="table">
<tr>
<th> @Html.DisplayNameFor(model => model.Name) </th>
<th> @Html.DisplayNameFor(model => model.InfoFields) </th>
<th> @Html.DisplayNameFor(model => model.Employment) </th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td> @Html.DisplayFor(mItem => item.Name) </td>
<td> @Html.EditorFor(mItem => item.InfoFields, new { htmlAttributes = new { @class = "form-control" } }) </td>
<td> @Html.DisplayFor(mItem => item.Employment) </td>
<td>
@Html.ActionLink("SAVE", "Save", new { id = item.ID }) |
@Html.ActionLink("EDIT", "Edit", new { id = item.ID })
</td>
</tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
Edit2,工作:
我的 table 循环现在看起来像:
@using(Html.BeginForm()) {
<table class="table">
@for(int i = 0; i < Model.Count; i++) {
<tr> <td>
@Html.TextBoxFor(m => m[i].Name)
</td> </tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
与 Stephens 回答的唯一区别是 for 循环之前的 @ 符号。
将视图中的模型更改为 IList<TileModel>
并在 for
循环中生成控件,以便它们使用索引器正确命名并且可以绑定到 post 上的集合返回
查看
@model IList<Anslagstavlan.Models.TileModel>
@using(Html.BeginForm())
{
....
for(int i = 0; i < Model.Count; i++)
{
@Html.TextBoxFor(m => m[i].InfoFields, new { @class = "form-control" })
.... // controls for other properties of your model that you want to post back
}
<input type="submit" value="Save" class="btn btn-default" />
}
POST方法
[HttpPost]
public ActionResult Index(List<TileModel> modelList)
{
// save each item and redirect
}
我希望能够在我的索引视图中按下提交按钮,这样可以保存通过我使用的 EditorFors 对列表所做的所有更改。
在编辑视图中,我可以一次编辑和保存一个列表项,但我希望能够一次单击保存完整列表。
我试过用 @using(Html.BeginForm()) {}
在索引视图中包装整个 table,然后在控制器中添加一个 HttpPost 方法,如下所示:
[HttpPost]
public ActionResult Index(List<TileModel> modelList) {
return View(modelList);
}
但是进入这个方法后modelList
为空。我试过将 [Bind("MyModel")]
添加到参数列表,但我对绑定什么有点无能为力..
我基本上是在尝试复制以下功能:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Employment")] TileModel mModel) {
if(ModelState.IsValid) {
db.Entry(mModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mModel);
}
,但让它在索引视图中保存完整列表。
知道如何实现这一目标吗?我是否走在正确的轨道上?
编辑:
这是我的索引视图(我的编辑视图与 visual studio 生成的视图基本没有变化,所以我认为展示它没有那么有趣?)。
@model IEnumerable<Anslagstavlan.Models.TileModel>
@{ ViewBag.Title = "title"; }
<ul class="nav navbar-nav">
<li>@Html.ActionLink("TILES", "tileboard", "tilemodels")</li>
<li>@Html.ActionLink("NEW ENTRY", "Create")</li>
</ul>
@using(Html.BeginForm()) {
<table class="table">
<tr>
<th> @Html.DisplayNameFor(model => model.Name) </th>
<th> @Html.DisplayNameFor(model => model.InfoFields) </th>
<th> @Html.DisplayNameFor(model => model.Employment) </th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td> @Html.DisplayFor(mItem => item.Name) </td>
<td> @Html.EditorFor(mItem => item.InfoFields, new { htmlAttributes = new { @class = "form-control" } }) </td>
<td> @Html.DisplayFor(mItem => item.Employment) </td>
<td>
@Html.ActionLink("SAVE", "Save", new { id = item.ID }) |
@Html.ActionLink("EDIT", "Edit", new { id = item.ID })
</td>
</tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
Edit2,工作:
我的 table 循环现在看起来像:
@using(Html.BeginForm()) {
<table class="table">
@for(int i = 0; i < Model.Count; i++) {
<tr> <td>
@Html.TextBoxFor(m => m[i].Name)
</td> </tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
与 Stephens 回答的唯一区别是 for 循环之前的 @ 符号。
将视图中的模型更改为 IList<TileModel>
并在 for
循环中生成控件,以便它们使用索引器正确命名并且可以绑定到 post 上的集合返回
查看
@model IList<Anslagstavlan.Models.TileModel>
@using(Html.BeginForm())
{
....
for(int i = 0; i < Model.Count; i++)
{
@Html.TextBoxFor(m => m[i].InfoFields, new { @class = "form-control" })
.... // controls for other properties of your model that you want to post back
}
<input type="submit" value="Save" class="btn btn-default" />
}
POST方法
[HttpPost]
public ActionResult Index(List<TileModel> modelList)
{
// save each item and redirect
}