views:

336

answers:

1

Hi

In the following code i want to save the value selected by user from drop downlist into database. but whatever value is selected by user, first value of dropdown lsit is saved to database

View

<% =Html.DropDownList("lstUsertype", (SelectList)ViewData["UserTypeID"])%>

Controller

public ActionResult CreateUser()
        {
            UmUser _UmUser = new UmUser();
            UMRepository _UMRepository = new UMRepository();
            EvoLetDataContext db = new EvoLetDataContext();
            ViewData["UserTypeID"] = new SelectList(_UMRepository.FillUserTypes(), "UserTypeID", "UserType",2);
            return View(_UmUser);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CreateUser(UmUser _umUser)
    {
        //try
        //{
            if (ModelState.IsValid)
            {

                //try
                //{
                    UserRepository _UserRepository = new UserRepository();

                    _UserRepository.Add(_umUser);
                    _UserRepository.Save();
                    return RedirectToAction("Details", new { id = _umUser.UserID });
                /*}
                catch
                {
                    ModelState.AddModelErrors(_umUser.GetRuleViolations());
                }*/
            }

            return View();
        //}
        /*catch
        {
            return View();
        }*/
    }
A: 

Hi,

This is how I'm doing it successfully:

<%= Html.DropDownListFor(model => model.Value, new SelectList(Values, "Key", "Value", Model.Value), "[select]")%>

Where Values is of type IDictionary and Value is of type Guid

Hope this helps!

Andreas