问题描述
在我的控制器中,我有一个存储在ViewBag中的代理列表
In my controller I have a list of Agents stored in ViewBag
ViewBag.Agents = new SelectList(db.Users.ToList(), "Id", "UserName", string.Empty);
在我的视图中,我已将其显示为DropDownListFor
In my View I have display this as a DropDownListFor
@Html.DropDownListFor(model => a.AgentList, ViewBag.Agents as SelectList, new { @class = "form -control", @style = "width:130px; height:30px" })
但是,我的列表没有将已选择的代理"显示为默认".我想念什么?如何设置下拉列表以将选定的座席显示为默认值?
However, my list is not showing the already selected Agent as Default. What am I missing? How can I set the Dropdownlist to display selected Agent as default?
我什至尝试过,但是没有用:
I even tried this but nothing work:
@Html.DropDownListFor(model => a.AgentList, new SelectList( ViewBag.Agents as SelectList, a.AgentName), new { @class = "form -control", @style = "width:130px; height:30px" })
这是我的模特
[Display(Name = "Agent ID")]
public string AgentID { get; set; }
[Display(Name = "Agent Name")]
public string AgentName { get; set; }
[Display(Name = "Agent")]
public List<ApplicationUser> AgentList { get; set; }
感谢您的帮助
推荐答案
请使用以下步骤绑定 DropDownListFor 中的选定值.
Please use the below step for bind the selected value in DropDownListFor.
步骤:-1 ViewBag.Agents= db.Users.ToList();
步骤:-2 @Html.DropDownListFor(model => Model.AgentID, new SelectList(ViewBag.Agents, "Id", "UserName",2))
这篇关于选择DropDownList的默认值使用ViewBag作为SelectList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!