DbUpdateException和Savechanges mvc编辑操作

时间:2016-12-06 09:01:58

标签: c# asp.net-mvc entity-framework controller savechanges

我试图更新mvc中的行(报表控制器中的编辑操作) 但是当我提交表单时,db.savechanges()中出现了下一个异常:

  

类型' System.Data.Entity.Infrastructure.DbUpdateException'的例外情况发生在EntityFramework.dll中但未在用户代码中处理

     

其他信息:更新条目时发生错误。有关详细信息,请参阅内部异常。)

编辑动作(GET):

 public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Report report = db.Reports.Find(id);
        if (report == null)
        {
            return HttpNotFound();
        }
        ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", report.DepartmentID);
        ViewBag.CreatedByID = new SelectList(db.Users, "UserID", "UserName", report.CreatedByID);
        return View(report);
    }

查看:

@model TheUnitOfEnternalAudit2.Report @{ ViewBag.Title = "تعديل تقرير"; }

<h2>تعديل</h2>
@using (Html.BeginForm()) { @Html.AntiForgeryToken()

<div class="form-horizontal">
  <h4>التقارير</h4>
  <hr />@Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.ReportID)

  <div class="form-group">

    @Html.Label("الإدارة", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.DropDownList("DepartmentID", null, htmlAttributes: new { @class = "form-control" })

      <div class="form-group">

        @Html.Label("اسم التقرير", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
          @Html.EditorFor(model => model.ReportName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ReportName, "", new { @class = "text-danger" })
        </div>
      </div>



      <div class="form-group">
        @*@Html.LabelFor(model => model.ReportDescription, htmlAttributes: new { @class = "control-label col-md-2" })*@ @Html.Label("الوصف", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">

          @Html.TextAreaFor(model => model.ReportDescription, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.ReportDescription, "", new { @class = "text-danger" })
        </div>
      </div>

      <div class="form-group">

        @Html.Label("تاريخ الإنشاء", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
          @Html.EditorFor(model => model.CreationDate, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.CreationDate, "", new { @class = "text-danger" })
        </div>
      </div>



      <div class="form-group">

        @Html.Label("الاعتماد", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
          <div class="checkbox">
            @Html.EditorFor(model => model.IsConfirmed) @Html.ValidationMessageFor(model => model.IsConfirmed, "", new { @class = "text-danger" })
          </div>
        </div>
      </div>

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

    <div>
      @Html.ActionLink("العودة للقائمة", "Index")
    </div>

    @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

编辑动作(帖子):

   [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ReportID,DepartmentID,ReportName,CreatedByID,ReportDescription,CreationDate,CreationDateHijri,IsConfirmed")] Report report)
    {
        if (ModelState.IsValid)
        {
            report.CreationDateHijri = report.CreationDate;
            db.Entry(report).State = EntityState.Modified;


            try
            {
                db.SaveChanges();// here is the Exception.
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var entityValidationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in entityValidationErrors.ValidationErrors)
                    {
                        Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                        TempData["error"] = "Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage;
                        return RedirectToAction("index");
                    }
                }
            }


            return RedirectToAction("Index");

        }
        ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", report.DepartmentID);
        ViewBag.CreatedByID = new SelectList(db.Users, "UserID", "UserName", report.CreatedByID);
        return View(report);
    }

我尝试运行一些代码,这些代码可以解决问题,但没有任何好处。

任何帮助?

0 个答案:

没有答案