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

Time:04-04

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 >
    <h4>Application</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.ApplicationID)

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

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

  

    <div >
        @Html.LabelFor(model => model.AppStatus, htmlAttributes: new { @class = "control-label col-md-2" })
        <div >
            @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 >
        <div >
            <input type="submit" value="Save"  />
        </div>
    </div>
</div>
}

CodePudding user response:

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();
   }
.....

CodePudding user response:

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

 
  • Related