asp.net-mvc 填充 ASP.NET MVC DropDownList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1297399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populating ASP.NET MVC DropDownList
提问by leebrandt
OK, I've been Googling for hours and trying everything and can't get anything to work. I am learning MVC using Sharp Architecture and have generated some basic forms for creating Client objects. I want to fill the state drop down list with a list of US states and let the user pick from that list. I am able to populate the list and get the value back (to save the client) but when I go to edit the client, the client's current state is not selected. I have set the selected value in the SelectList:
好吧,我已经用谷歌搜索了几个小时并尝试了所有方法,但什么也做不了。我正在使用 Sharp Architecture 学习 MVC,并生成了一些用于创建 Client 对象的基本表单。我想用美国州的列表填充州下拉列表,并让用户从该列表中进行选择。我能够填充列表并取回值(以保存客户端)但是当我去编辑客户端时,客户端的当前状态未被选中。我已经在 SelectList 中设置了选定的值:
<li>
<label for="Client_StateProvince">StateProvince:</label>
<div>
<%= Html.DropDownListFor(c=>c.Client.StateProvince, new SelectList(Model.StateProvinces, "id", "Name", Model.Client.StateProvince), "-- Select State --")%>
</div>
<%= Html.ValidationMessage("Client.StateProvince")%>
</li>
This does not seem to be good enough. What am I missing?
这似乎还不够好。我错过了什么?
回答by leebrandt
<%= Html.DropDownListFor(c => c.Client.StateProvince.Id,
new SelectList(Model.StateProvinces,
"id",
"Name",
Model.Client.StateProvince),
"-- Select State --")%>
This does it.
这样做。
Hope this helps someone else.
希望这对其他人有帮助。
~Lee
~李
回答by leebrandt
Worked perfectly for me thanx! I used it to set a parent relation on a subcategory:
非常适合我thanx!我用它在子类别上设置父关系:
<%= Html.DropDownListFor(
model => model.Category.ParentId,
new SelectList(Model.Categories,
"CategoryId",
"Name",
Model.Categories.Where(x => x.CategoryId == Model.Category.ParentId).Single()))%>
Jeroen
杰伦
回答by bcahill
I did it this way. Works well.
我是这样做的。效果很好。
Controller
控制器
IFarmModelInterface service2 = new FarmModelRepository();
ViewData["Farms"] = new SelectList(service2.GetFarmNames(), "id", "FarmName", "XenApp");
View
看法
<%: Html.DropDownListFor(m => m.Farm, (ViewData["Farms"] as SelectList)) %>
回答by Jasvir Singh
public ActionResult AllUsers()
{
List<Users> users = userRep.GetUsers();
var listUsers = (from u in users.AsEnumerable()
select new SelectListItem
{
Text = u.UserName,
Value = u.UserId.ToString(),
Selected = (u.UserId==6)
}).AsEnumerable();
ViewBag.ListItems = listUsers;
//ViewBag.SelectedItem = 2;
return View();
}
In AllUsers.cshtml
在 AllUsers.cshtml 中
<p>@Html.DropDownList("ListItems")</p>
回答by Dennis Shtemberg
<%= Html.DropDownListFor(c => c.Client.StateProvince, new SelectList(Model.StateProvinces, "Id", "Name")) %>
and override ToString() for StateProvince to return Id, i.e.:
并为 StateProvince 覆盖 ToString() 以返回 Id,即:
return Id.ToString();
This works but is not a perfect solution...
这有效,但不是完美的解决方案......
Dennis
丹尼斯
回答by Thisara Subath
This is View- MVC Controller
这是视图- MVC 控制器
@Html.DropDownListFor(m => m.Entity, new ABC.Models.DropDownPopulate().MyMethod, new { @class = "form-control input-inline input-medium" })
MyMethodGet Data List Bind With Dropdown Using SelectListItems
MyMethod使用 SelectListItems 获取数据列表绑定与下拉列表
public List<SelectListItem> MyMethod
{
get
{
List<SelectListItem> dropdownList = new List<SelectListItem>();
var items = new DropDown_Bl().GetAll();
foreach (var item in items)
{
SelectListItem dropdownItem = new SelectListItem();
dropdownItem.Value = item.RnID.ToString();
dropdownItem.Text = item.Description;
dropdownList.Add(dropdownItem);
}
dropdownList.Insert(0, new SelectListItem() { Text = "Please Select", Value = "", Selected = true });
return dropdownList;
}
}
GetAll Method In Dropdown_Bl- Used to get data as list from database
Dropdown_Bl 中的 GetAll 方法- 用于从数据库中以列表形式获取数据
public List<My_Table> GetAll()
{
var Items = _Context.MyRepository.GetMany(q => q.Area == "ASD").ToList();
return Items ;
}
回答by Krishan Pal
public ActionResult AllUsers() {
List<Users> users = userRep.GetUsers();
var listUsers = (from u in users.AsEnumerable()
select new SelectListItem
{
Text = u.UserName,
Value = u.UserId.ToString(),
Selected = (u.UserId==6)
}).AsEnumerable();
// ViewBag.ListItems = listUsers;
ViewData["tempEmpList"]=listUsers;
return View(); }
Fill Dropdown
填充下拉菜单
@Html.DropDownList("SelectedEmployee", new SelectList((IEnumerable) ViewData["tempEmpList"], "Id", "Name"))
@Html.DropDownList("SelectedEmployee", new SelectList((IEnumerable) ViewData["tempEmpList"], "Id", "Name"))

