在以下代码中,我想将用户选择的值从下拉列表保存到数据库中。但无论用户选择什么值,dropdown lsit的第一个值都会保存到数据库
查看
<% =Html.DropDownList("lstUsertype", (SelectList)ViewData["UserTypeID"])%>
控制器
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();
}*/
}
答案 0 :(得分:0)
这就是我成功的方式:
<%= Html.DropDownListFor(model => model.Value, new SelectList(Values, "Key", "Value", Model.Value), "[select]")%>
其中Values的类型为IDictionary,Value的类型为Guid
希望这有帮助!