Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:22:48+00:00 2026-06-10T21:22:48+00:00

I have looked through a ton of tutorials and suggestions on how to work

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:22:49+00:00Added an answer on June 10, 2026 at 9:22 pm

    You do not need to set the whole Color object. Just set the ColorId property.

    Change

    itemmodel.Color = db.Colors.Find(itemEditModel.ColorID);
    

    To

    itemmodel.ColorId = itemEditModel.ColorID;
    

    Edit

    Note that your database does not store the whole object. The Color object in ItemModel is just a convenient way to access the ColorModel entity that is assosiated by a foreign key.

    According to convention, the name of the foreign key property should be ColorId. Add this int property in your ItemModel class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked through a number of tutorials, but I still can't figure out
I have looked through replacing colors in an image but cannot get it to
I have looked through most of the blog postings here dealing with MVC and
I've downloaded the Windows Media Center SDK and have looked through the documentation, but
I've looked through several posts but I haven't quite found any answers that have
i have looked through other scanf posts but i cant seem to find the
I have looked through many tutorials, as well as other question here on stack
I have looked through the help documentation in netbeans 6.5.1 but cannot find anything
i have looked through other related questions but none of them giving me right
I have looked through various answers here, but am still unable to figure it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.