更新相同类型的多个项目

时间:2013-02-27 20:16:05

标签: c# .net asp.net-mvc razor asp.net-mvc-4

我正在使用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条目。”

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

第一个问题:您的观点涉及多个 ViewPerm,但您的帖子操作只需一个

更改帖子方法以处理多个:

[HttpPost]
public ActionResult Index(IEnumerable<ViewPerm> viewperms)
{
    if (ModelState.IsValid)
    {
        foreach (ViewPerm viewperm in viewperms)
        {
            db.Entry(viewperm).State = EntityState.Modified;
        }

        db.SaveChanges();
    }

    return View(viewperms);
}

您的第二个问题是,您正在使用foreach在视图中迭代模型中的项目。这些字段需要正确编制索引,以便模型绑定器可以完成它(它适用于数组)。将您的观点更改为:

@for (int i = 0; i < Model.Count(); i++)
{
    <tr>
        <td style="border: 5px solid white;">
            @Html.DisplayFor(m => m[i].Roles.Role)
        </td>
        <td style="border: 5px solid white;">
            @Html.DisplayFor(m => m[i].Views.Folder)
            @Html.DisplayFor(m => m[i].Views.Page)
        </td>
        <td style="border: 5px solid white;">
            @Html.EditorFor(m => m[i].Perm)
        </td>
        <td>                        
            @Html.ActionLink("Delete", "Delete", new { id = Model[i].VPermID })
        </td>
    </tr>
}