asp.net-mvc 将 Html.DropDownList 与静态项绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5576257/
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
Bind Html.DropDownList with static items
提问by Mukesh
I have to bind an Html.DropDownList
with just two items statically.
我必须Html.DropDownList
静态绑定一个只有两个项目。
Text="Yes" Value="1"
Text="No" Value="0"
The important thing is that, I have to set the text and value fields.
重要的是,我必须设置文本和值字段。
How can I do this?
我怎样才能做到这一点?
回答by Saurabhbhatt
I used this is properly working
我用过这是正常工作
@Html.DropDownList("Status", new List<SelectListItem>
{
new SelectListItem{ Text="Active", Value = "1" },
new SelectListItem{ Text="Not-Active", Value = "0" }
})
回答by Sreekumar P
It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.
最好不要在视图中创建 SelectList。您应该在控制器中创建它并使用 ViewData 传递它。
Example:
例子:
var list = new SelectList(new []
{
new { ID = "1", Name = "name1" },
new { ID = "2", Name = "name2" },
new { ID = "3", Name = "name3" },
},
"ID", "Name", 1);
ViewData["list"]=list;
return View();
you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.
您传递给构造函数:IEnumerable 对象、值字段、文本字段和所选值。
in the View:
在视图中:
<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
回答by sarvesh
Code below assumes you are using razor view engine if not you will need to convert it.
下面的代码假设您使用的是剃刀视图引擎,否则您将需要转换它。
@{
var listItems = new List<ListItem>();
listItems.Add(new ListItem{Text="Yes", Value="1"});
listItems.Add(new ListItem{Text="No", Value="0"});
}
@Html.DropDownListFor(m=>m.SelectedValue, listItem);
You should consider creating the model in your code instead of the view. Also this would be a good candidate for an editor template.
您应该考虑在代码而不是视图中创建模型。这也是编辑器模板的一个很好的候选者。
回答by Idrees Khan
if you want to be alittle explicity then try
如果你想明确一点,那就试试
@{
var domainsList = new SelectList(new []
{
new SelectListItem { Text = ".Com", Value = ".com", Selected = true },
new SelectListItem { Text = ".Shopping", Value = ".shopping"},
new SelectListItem { Text = ".Org", Value = ".org"},
new SelectListItem { Text = ".Net", Value = ".net"},
new SelectListItem { Text = ".AE", Value = ".ae"},
new SelectListItem { Text = ".Info", Value = ".info"},
}, "Value", "Text");
}
@Html.DropDownList("TopLevelDomains", domainsList)
回答by Paul Makram Fares
This solved it for me:
这为我解决了:
<td>
@{ var RlistItems = new List<SelectListItem>();
RlistItems.Add(new SelectListItem { Text = "Select Room Type", Value = "0" });
RlistItems.Add(new SelectListItem { Text = "Meeting Room", Value = "1" });
RlistItems.Add(new SelectListItem { Text = "Office", Value = "2" });
RlistItems.Add(new SelectListItem { Text = "Cafeteria", Value = "3" });
}
@Html.DropDownListFor(model=>model.FirstOrDefault().RoomType
,RlistItems,RlistItems[item.RoomType.Value].Selected=true )
</td>
回答by Siva Ragu
<select asp-for="CountryName" asp-items=@(new List<SelectListItem> { new SelectListItem {Text="India",Value="1" } ,new SelectListItem {Text="Japan",Value="2" }} ) class="form-control">
<option>SELECT COUNTRY -- </option>