ASP.net MVC DropLownList db.SaveChanges not saving selection

Posted by WMIF on Stack Overflow See other posts from Stack Overflow or by WMIF
Published on 2012-09-07T02:28:06Z Indexed on 2012/09/07 3:38 UTC
Read the original article Hit count: 251

I have looked through a ton of tutorials and suggestions on how to work with DropDownList in MVC. I was able to get most of it working, but the selected item is not saving into the database. I am using MVC 3 and Razor for the view.

My DropDownList is getting created with the correct values and good looking HTML. When I set a breakpoint, I can see the correct selected item ID in the model getting sent to controller. When the view goes back to the index, the DropDownList value is not set. The other values save just fine.

Here are the related views. The DropDownList is displaying a list of ColorModel names as text with the ID as the value.

public class ItemModel
{
    [Key]
    public int ItemID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public virtual ColorModel Color { get; set; }
}
public class ItemEditViewModel
{
    public int ItemID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int ColorID { get; set; }
    public IEnumerable<SelectListItem> Colors { get; set; }
}
public class ColorModel
{
    [Key]
    public int ColorID { get; set; }
    public string Name { get; set; }
    public virtual IList<ItemModel> Items { get; set; }
}

Here are the controller actions.

    public ActionResult Edit(int id)
    {
        ItemModel itemmodel = db.Items.Find(id);
        ItemEditViewModel itemEditModel;
        itemEditModel = new ItemEditViewModel();
        itemEditModel.ItemID = itemmodel.ItemID;
        if (itemmodel.Color != null) {
            itemEditModel.ColorID = itemmodel.Color.ColorID;
        }
        itemEditModel.Description = itemmodel.Description;
        itemEditModel.Name = itemmodel.Name;
        itemEditModel.Colors = db.Colors
            .ToList()
            .Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.ColorID.ToString()
                });
        return View(itemEditModel);
    }

    [HttpPost]
    public ActionResult Edit(ItemEditViewModel itemEditModel)
    {
        if (ModelState.IsValid)
        {
            ItemModel itemmodel;
            itemmodel = new ItemModel();
            itemmodel.ItemID = itemEditModel.ItemID;
            itemmodel.Color = db.Colors.Find(itemEditModel.ColorID);
            itemmodel.Description = itemEditModel.Description;
            itemmodel.Name = itemEditModel.Name;
            db.Entry(itemmodel).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(itemEditModel);
    }

The view has this for the DropDownList, and the others are just EditorFor().

@Html.DropDownListFor(model => model.ColorID, Model.Colors, "Select a Color")

When I set the breakpoint on the db.Color.Find(...) line, I show this in the Locals window for itemmodel.Color:

{System.Data.Entity.DynamicProxies.ColorModel_0EB80C07207CA5D88E1A745B3B1293D3142FE2E644A1A5202B90E5D2DAF7C2BB}

When I expand that line, I can see the ColorID that I chose from the dropdown box, but it does not save into the database.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-3

Related posts about entity-framework