简体   繁体   中英

The UPDATE statement conflicted with the FOREIGN KEY constraint in asp.net mvc 5

I am getting this error:

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_TitleMember". The conflict occurred in database "ParishDBSQL", table "dbo.Titles", column 'title_id'. The statement has been terminated.

controller

Get Action

public ActionResult Edit(int? id)
    {
       var member = (from h in db.Members
       join f in db.Titles on h.title_id equals f.title_id
                       where h.m_id == id
                      select new
                      {
                          title_id = h.title_id,
                        }).First();
var viewmodel = new MembersViewModel();
        viewmodel.title_id = member.title_id;
ViewBag.Titles = new SelectList(db.Titles.ToList(), "title_id", "Titles", viewmodel.title_id);

return View(viewmodel);

    }

Post Action

public ActionResult Edit(MembersViewModel vm)
    {
        if (ModelState.IsValid)
        {
var member = db.Members.Find(vm.m_id);
          member.title_id = vm.title_id;
   ViewBag.Titles = new SelectList(db.Titles.ToList(), "title_id", "Titles", vm.title_id);

                db.Members.Attach(member);
                db.Entry(member).State = EntityState.Modified;    
 db.SaveChanges();

                return RedirectToAction("Index");

        }

        return View(vm);

View

  <div class="col-md-4">
             <div class="form-group">
                <label>Title</label>

                @Html.DropDownList("Titles")
                @Html.ValidationMessageFor(model => model.title_id)
            </div>
        </div>

Model

 public partial class Title
    {
    public Title()
    {
        this.Heads = new HashSet<Head>();
        this.Members = new HashSet<Member>();
    }

    public int title_id { get; set; }
    public string Titles { get; set; }

    public virtual ICollection<Head> Heads { get; set; }
    public virtual ICollection<Member> Members { get; set; }
}

Viewmodel

public class MembersViewModel
{
    public int m_id { get; set; }
    public string titles { get; set; }
    public int title_id { get; set; }
}

I want to update a title_id using a dropdownlist on my members table table but I get the error that is shown above.

i think that the error is in this line

member.title_id = vm.title_id

maybe vm.title_id it is not found in primary table and it could not be a foreign key. Check the correct value of this variable

if it doesn't solve your problem looking at your code I saw some thinghs that I didn't understand very well.

try to change your code in this way

if (ModelState.IsValid)
{
    var member = db.Members.Find(vm.m_id);
    member.title_id = vm.title_id;
    ViewBag.Titles = new SelectList(db.Titles.ToList(), "title_id", "Titles", vm.title_id);   
    db.SaveChanges();
    return RedirectToAction("Index");
}
  1. Usually I use db.Entry(member).State = EntityState.Modified; if I have to do other changes before db.SaveChanges();

  2. member is already attched to your table. Why do you db.Members.Attach(member);

update

change

@Html.DropDownListFor(m => m.title_id, ViewBag.Titles)

into

@Html.DropDownListFor(m => m.title_id, (SelectList) ViewBag.Titles)

because ViewBag is a dynamic object

The problem is the drop down list in the view. You really want to initialize it like this:

@Html.DropDownListFor(m => m.title_id, ViewBag.Titles)

The way you're initializing it now causes the selected value to be passed as a query string parameter - you should see the URL being posted to as ...?Titles=1 for example.

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