【问题标题】:Error when in the edit view in rhe savechanges在 savechanges 的编辑视图中时出错
【发布时间】:2019-06-04 09:40:09
【问题描述】:

我有一个编辑视图,可以编辑电影表中的数据,该视图打开时没有错误,但是当我尝试保存更改时,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 编辑模板:

@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; }
    }

【问题讨论】:

  • 抛出异常时moviesData.MovieID的值是多少?你认为为什么会这样?
  • 请在if(ModelState.IsValid) 上设置断点。运行到断点。转到Immediate Window 并输入?moviesData.MovieID。显示什么?

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


【解决方案1】:

在您看来,您没有为您的MovieId 找到任何输入(EditorFor/Hiddenfor),此外,当您发布表单时,MovieId 的值在您的控制器方法中为 0,因此您面临错误。为了解决这个问题,把下面的代码 sn-p 放在你的视图中:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多