未能在我的应用程序中实现编辑功能 - dbUpdateEntityConcurrencyException;

时间:2016-11-26 12:39:38

标签: asp.net-mvc entity-framework

我正在使用aps.net mvc和Entity框架构建(用于学习目的)一个Web应用程序。

在我的一个控制器中,我有一个编辑方法和相应的自动生成视图。到目前为止,我所做的一切似乎都很好,但出于某种原因,当我尝试从我的数据库mu实际编辑某些东西时,db.SaveChanges();控制器中的方法,我不知道为什么。

警告是:dbUpdateEntityConcurrencyException未安全

Bellow我将发布我的控制器的代码并查看:

private PostContext db = new PostContext();

     public ActionResult Edit(Post post)
        {
             if (!ModelState.IsValid)
            {
                return View(post);
            }
            db.Entry(post).State = EntityState.Modified;
            db.SaveChanges();
            return View();
        }

观点:

    @model class_project.Models.Post

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.PostId)

        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

有人可以帮忙吗?

0 个答案:

没有答案