asp.net-mvc 如何从模型中为 ASP.NET MVC DropDownList 设置默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22991387/
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
How to set default value for ASP.NET MVC DropDownList from model
提问by Thomas
i am new in mvc. so i populate dropdown this way
我是 mvc 新手。所以我以这种方式填充下拉列表
public ActionResult New()
{
var countryQuery = (from c in db.Customers
orderby c.Country ascending
select c.Country).Distinct();
List<SelectListItem> countryList = new List<SelectListItem>();
string defaultCountry = "USA";
foreach(var item in countryQuery)
{
countryList.Add(new SelectListItem() {
Text = item,
Value = item,
Selected=(item == defaultCountry ? true : false) });
}
ViewBag.Country = countryList;
ViewBag.Country = "UK";
return View();
}
@Html.DropDownList("Country", ViewBag.Countries as List<SelectListItem>)
i like to know how can i populate dropdown from model and also set default value. any sample code will be great help. thanks
我想知道如何从模型填充下拉列表并设置默认值。任何示例代码都会有很大帮助。谢谢
回答by Menelaos Vergis
Well this is not a great way to do this.
那么这不是一个很好的方法来做到这一点。
Create a ViewModel that will hold everything you want to be rendered at the view.
创建一个 ViewModel,它将保存您想要在视图中呈现的所有内容。
public class MyViewModel{
public List<SelectListItem> CountryList {get; set}
public string Country {get; set}
public MyViewModel(){
CountryList = new List<SelectListItem>();
Country = "USA"; //default values go here
}
Fill it with the data you need.
用你需要的数据填充它。
public ActionResult New()
{
var countryQuery = (from c in db.Customers
orderby c.Country ascending
select c.Country).Distinct();
MyViewModel myViewModel = new MyViewModel ();
foreach(var item in countryQuery)
{
myViewModel.CountryList.Add(new SelectListItem() {
Text = item,
Value = item
});
}
myViewModel.Country = "UK";
//Pass it to the view using the `ActionResult`
return ActionResult( myViewModel);
}
At the view, declare that this view is expecting a Model with type MyViewModel using the following line at the top of the file
在视图中,使用文件顶部的以下行声明此视图需要类型为 MyViewModel 的模型
@model namespace.MyViewModel
And at anytime you may use the Model as you please
您可以随时随意使用模型
@Html.DropDownList("Country", Model.CountryList, Model.Country)
回答by Chris Pratt
You can't set the default value using Html.DropDownList, if you want to have a default value, the property itself should have a default value.
你不能使用 设置默认值Html.DropDownList,如果你想有一个默认值,属性本身应该有一个默认值。
private string country;
public string Country
{
get { return country ?? "UK"; }
set { country = value; }
}
Then, when the drop down list renders, as long as "UK" is actually a value for one of the options, it will be automatically set to that.
然后,当下拉列表呈现时,只要“UK”实际上是其中一个选项的值,它就会自动设置为该值。
回答by Pajoc
If the DropDownListis filled in the controller and sent to the view via ViewBag, you can do:
如果DropDownList填写在控制器中并通过 发送到视图ViewBag,您可以执行以下操作:
ViewBag.MyName = new SelectList(DbContextname.Tablename, "Field_ID", "Description",idtobepresented);

