默认情况下,我无法选择下拉列表。
模特的标题属性先生。但下拉列表不会选择它。
CSHTML -
@Html.TextBoxFor(model=>model.Title)
@Html.DropDownListFor(model => model.Title, @ViewBag.All_Title as SelectList, new { @class = "form-control" })
CS代码 -
ViewBag.All_Title = Titles(model.Title);
标题方法 -
public static SelectList Titles(string selectedValue)
{
List<SelectListItem> titles = new List<SelectListItem>();
titles.Add(new SelectListItem { Text = "Prof.", Value = "Prof." });
titles.Add(new SelectListItem { Text = "Dr.", Value = "Dr." });
titles.Add(new SelectListItem { Text = "Miss", Value = "Miss" });
titles.Add(new SelectListItem { Text = "Mr.", Value = "Mr." });
titles.Add(new SelectListItem { Text = "Mrs.", Value = "Mrs." });
titles.Add(new SelectListItem { Text = "Ms.", Value = "Ms." });
SelectList sl = new SelectList(titles, "Value", "Text");//, selectedValue);
return sl;
}
模型是 -
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
public SelectList All_Title { get; set; }
更新
调试器显示先生&#39;是selectedValue但它不会在网页上保持选中状态。
答案 0 :(得分:0)
试试这个:
@Html.DropDownList("All_Title", null, htmlAttributes: new { @class = "form-control" })
您需要在模型中保留所选的项目ID:
标题方法:
public static SelectList Titles(string selectedValue)
{
List<SelectListItem> titles = new List<SelectListItem>();
titles.Add(new SelectListItem { Text = "Prof.", Value = "1" });
titles.Add(new SelectListItem { Text = "Dr.", Value = "2" });
titles.Add(new SelectListItem { Text = "Miss.", Value = "3" });
titles.Add(new SelectListItem { Text = "Mr.", Value = "4" });
titles.Add(new SelectListItem { Text = "Mrs.", Value = "5" });
titles.Add(new SelectListItem { Text = "Ms.", Value = "6" });
SelectList sl = new SelectList(titles, "Value", "Text", selectedValue);
return sl;
}
<强>型号:强>
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
public int All_Title_Id { get; set; }
<强>控制器:强>
ViewBag.All_Title_Id = Titles(model.Title);
查看:强>
@Html.DropDownList("All_Title_Id", null, htmlAttributes: new { @class = "form-control" })
答案 1 :(得分:0)
我们在项目中使用这个。
@Html.DropDownListFor(model => Model.Client.ID, new SelectList(Model.Clients, "ID", "Name", Model.Client), "- All -", new { @class = "projects-drop-down", @id = "clientListDropDown"})
所以,你的下拉列表应该是这样的
@Html.DropDownListFor(model => model.Title, new SelectList(ViewBag.All_Title, "Value", "Text", ViewBag.YourSelectListItemProperty), "- Select -", new { @class = "projects-drop-down", @id = "clientListDropDown"})
答案 2 :(得分:0)
这是怎么回事?
public static SelectList Titles(string selectedValue)
{
List<SelectListItem> titles = new List<SelectListItem>();
titles.Add(new SelectListItem { Text = "Prof.", Value = "Prof." });
titles.Add(new SelectListItem { Text = "Dr.", Value = "Dr." });
titles.Add(new SelectListItem { Text = "Miss", Value = "Miss" });
titles.Add(new SelectListItem { Text = "Mr.", Value = "Mr.", Selected = true });
titles.Add(new SelectListItem { Text = "Mrs.", Value = "Mrs." });
titles.Add(new SelectListItem { Text = "Ms.", Value = "Ms." });
SelectList sl = new SelectList(titles, "Value", "Text");//, selectedValue);
return sl;
}