2
我正在使用MVC 4,我想擺脫編輯頁面,只是在索引頁面上同時更改我想要的多個項目。更新同一類型的多個項目
這裏是CSHTML:
@model IEnumerable<HelpDesk.Models.ViewPerm>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>ViewPerm</legend>
<table style="border: 5px solid white;">
<tr>
<th style="border: 5px solid white;">
@Html.DisplayNameFor(model => model.Roles.Role)
</th>
<th style="border: 5px solid white;">
@Html.DisplayNameFor(model => model.Views.Page)
</th>
<th style="border: 5px solid white;">
@Html.DisplayNameFor(model => model.Perm)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td style="border: 5px solid white;">
@Html.DisplayFor(modelItem => item.Roles.Role)
</td>
<td style="border: 5px solid white;">
@Html.DisplayFor(modelItem => item.Views.Folder)
@Html.DisplayFor(modelItem => item.Views.Page)
</td>
<td style="border: 5px solid white;">
@Html.EditorFor(modelItem => item.Perm)
</td>
<td>
@Html.ActionLink("Delete", "Delete", new { id = item.VPermID })
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
和C#:
public ActionResult Index()
{
var viewperm = db.ViewPerm.Include(v => v.Roles).Include(v => v.Views);
return View(viewperm.ToList());
}
[HttpPost]
public ActionResult Index(ViewPerm viewperm)
{
if (ModelState.IsValid)
{
db.Entry(viewperm).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(viewperm);
}
當我打的保存按鈕它說:「商店更新,插入或刪除語句影響行的一個意外的數(0)。實體可能已被修改或刪除,因爲實體已加載。刷新ObjectStateManager條目。「
感謝您的幫助。
我在** M [I] ** Roles.Role和那些喜歡它,碰上在CSHTML **型號[I] **說:「可以不適用索引。與[]到一個表達式'System.Collections.Generic.IEnumerable' –
user2116960
2013-02-27 20:56:20
@ user2116960改變你的模型和你的動作方法是一個'List'而不是'IEnumerable' –
mattytommo
2013-02-27 20:58:06
對不起, – user2116960 2013-02-27 21:01:18