开发者

ASP .net Dropdown list not saving the selected value to database

开发者 https://www.devze.com 2022-12-28 18:19 出处:网络
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

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 Vi开发者_StackOverflow社区ew(_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();
        }*/
    }


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消