I really don't understand why this kind of error happens..
As usual, By Scaffolding model(In my case, "person" model), Visual Studio generate CRUD View...
and I made some variation to that view.
Now, Because I didn't want to update all the stuff in one page, I have made 'Second Edit' View.
(well. and I also have want to make some cascading like "dropdownlist" in server side, so I make 'Second Edit' View.
But, The Second Edit View can't update values.
It's error message is this :
SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Person_College_CollegeID". The conflict occurred in database "Nugu3", table "dbo.College", column 'ID'.
The statement has been terminated.
The first scaffolded 'Edit' View and action method works well.
Why it happend?
My 'secondEdit' method is like this.
// 세부사항 에디트 Get public async Task<IActionResult> EditSecond(int? id) { if (id == null) { return NotFound(); } var person = await _context.Person .AsNoTracking() .SingleOrDefaultAsync(m => m.ID == id); if (person == null) { return NotFound(); } PopulateCollegeDropDownList(person.CollegeID); PopulateHighSchoolDropDownList(person.HighschoolID); PopulateJobDropDownList(person.job); PopulateIdentityDropDownList(person.job, person.Chulsin); PopulateGradeDropDownList(person.job, person.grade); return View(person); } // 세부사항 에디트 Post [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> EditSecond(int id, [Bind("ID, Name,Chulsin, grade, GrandWork, job, CollegeID, SchoolID")] Person person) { if (id != person.ID) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(person); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(person.ID)) { return NotFound(); } else { throw; } } return RedirectToAction("Index"); } PopulateCollegeDropDownList(person.CollegeID); PopulateHighSchoolDropDownList(person.HighschoolID); PopulateJobDropDownList(person.job); PopulateIdentityDropDownList(person.job, person.Chulsin); PopulateGradeDropDownList(person.job, person.grade); return View(person); } // 출신 드롭다운리스트 private void PopulateIdentityDropDownList(string selectedJob = null, string selectedIdentity = null) { List<string> identity = new List<string> { "순경공채", "조사특채", "간부후보", "경찰대", "사시특채", "로스쿨특채", "기타" }; switch(selectedJob) { case "경찰": identity = new List<string> { "순경공채", "조사특채", "간부후보", "경찰대", "사시특채", "로스쿨특채", "기타" }; break; case "판사": identity = new List<string> { "사법시험", "로스쿨", "고등고시"}; break; case "검사": identity = new List<string> { "사법시험", "로스쿨", "고등고시"}; break; case "변호사": identity = new List<string> { "사법시험", "로스쿨", "고등고시"}; break; default : identity = new List<string> { "일반인" }; break; } ViewBag.Chulsin = new SelectList(identity, selectedIdentity); }
Plz, I'm stucked. and Can't understand that kind of SQL error..