mvc4在控制器中绑定@ Html.DropDownListFor

时间:2014-02-06 00:51:57

标签: asp.net-mvc-4 html.dropdownlistfor

如何将@ Html.DropDownListFor绑定到来自模型的值? 我的模特:

public UpdatePersonalModel()
{
  Titles = new List<SelectListItem>(); 
  Titles.Add(new SelectListItem{Text = "Mr",Value = "Mr"});
  Titles.Add(new SelectListItem{Text = "Mrs",Value = "Mrs"});
  Titles.Add(new SelectListItem{Text = "Miss",Value = "Miss"});
  Titles.Add(new SelectListItem{Text = "Ms",Value = "Ms"});
  Titles.Add(new SelectListItem{Text = "Dr",Value = "Dr"});
  Titles.Add(new SelectListItem{Text = "Hon",Value = "Hon"});
  Titles.Add(new SelectListItem{Text = "Prof",Value = "Prof"});
}
public List<SelectListItem> Titles { get; set; }
public string Title { get; set; }
...
...

控制器:

public ActionResult UpdatePersonal()
{
    //...
    var model = new UpdatePersonalModel();
    model.Title = webContext.CurrentUser.Title;
    model.FirstName = webContext.CurrentUser.First_Name;
    model.MiddleName = webContext.CurrentUser.q_Middle_Name;
    //...

    return View(model);
}
视图中的

: 当我有模型时。标题会显示模型值,但它会复制值并且不会绑定 @Html.DropDownListFor(m => m.Title, Model.Titles, Model.Title,new { @class = "EnrtyFeildSelect" })这里总是带来第一个值 @Html.DropDownListFor(m => m.Title, Model.Titles, new { @class = "EnrtyFeildSelect" })

0 个答案:

没有答案