简体   繁体   中英

The UPDATE statement conflicted with the FOREIGN KEY constraint MVC Web Application

Context:

I want a user to only update one part [AppStatus] of an entry. However, when I test it, I get the error mentioned in the subject. I've seen similar questions asked, and tried doing some of the steps they outlined (using CASCADE for the FK, for instance). But it didn't work. So it's probably something in the code that I messed up with. I thought maybe, if I adjust the Bind to only include [AppStatus] that would do it, but that didn't work either.

The error throws on two different FKs [StudentID] and [JobPostingID].

Code:

Controller:

 public ActionResult Edit([Bind(Include = "ApplicationID,StudentID,JobPostingID,Resume,AppStatus")] Application application)
        {
            if (ModelState.IsValid)
            {
                db.Entry(application).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FirstName", application.StudentID);
            ViewBag.JobPostingID = new SelectList(db.JobPostings, "JobPostingID", "Position", application.JobPostingID);
            return View(application);
        }

View:

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

    <div class="form-group">
        @Html.LabelFor(model => model.Student.FirstName, "FirstName", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.Student.FirstName)
        </div>
    </div>

 
    <div class="form-group">
        @Html.LabelFor(model => model.JobPosting.Position, "Position", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.JobPosting.Position)
        </div>
    </div>

  

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

I usually use this syntax, especially if I want to update only certain properties

public ActionResult Edit( Application application)
        {
if (ModelState.IsValid)
{
  var existedApplication=db.Applications.FirstOrDefault(i=> i.ApplicationID=application.ApplicationID);
     if(existedApplication!=null)
    {
   existedApplication.AppStatus = application.ApplicationStatus;
   db.Entry(existedApplication).Property(i => i.ApplicationStatus).IsModified = true;
   var result = db.SaveChanges();
   }
.....

Serge's answer pretty much fixed my issue- but I did need to make a couple of adjustments to get it to run.

  public ActionResult Edit(Application application)
    {
        if (ModelState.IsValid)
        {
            var existedApplication = db.Applications.FirstOrDefault(i => i.ApplicationID == application.ApplicationID);
            if (existedApplication != null)
            {
                existedApplication.AppStatus = application.AppStatus;
                db.Entry(existedApplication).Property(i => i.AppStatus).IsModified = true;
                var result = db.SaveChanges();
                return RedirectToAction("Index");
            }
           
            return View(application);
        }
        return View(application);
    }

 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM