在rhe savechanges的编辑视图中时出错

时间:2019-06-04 09:40:09

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

我有一个Edit视图,该视图可编辑Movie表中的数据,该视图打开时没有错误,但是当我尝试保存更改时,在db.SaveChanges();中出现一个错误。

这就是我在控制器中拥有的:

        public ActionResult Edit(int? Id)
        {

            if (Id == null)

                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            MoviesData movie = db.MoviesData.Find(Id);

            if (movie == null)

                return HttpNotFound();

            return View(movie);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "MovieID, MovieName, MovieCategory, MovieDescription, MovieYear")] MoviesData moviesData)
        {
                if(ModelState.IsValid)
                {
                    db.Entry(moviesData).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(moviesData);

我的看法是普通的MVC Edit模板:

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

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



------->@Html.HiddenFor(model => model.MovieName)



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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.MovieYear, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MovieYear, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.MovieYear, "", 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>

db.SaveChanges();输出此错误:

System.Data.Entity.Core.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

我已经阅读了该链接中的内容,但我不太了解,该怎么办?另外,您需要有关我的计划的更多信息吗?

编辑:

电影模型:

public class MovieViewModel
    {
        public int MovieID { get; set; }
        public string MovieName { get; set; }
        public string MovieDescription { get; set; }
        public string MovieCategory { get; set; }
        public string MovieYear { get; set; }
    }

1 个答案:

答案 0 :(得分:3)

在您看来,您没有找到MovieId的任何输入(EditorFor / Hiddenfor),此外,在您发布表单时,在您的方法中MovieId的值为0控制器,使您面临错误。要解决此问题,请在您的视图下面添加以下代码段:

@Html.HiddenFor(model => model.MovieId)

我认为它可以解决您的问题。