asp.net-mvc MVC5 - 如何在 DropDownListFor Html helper 中设置“selectedValue”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41719293/
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
MVC5 - How to set "selectedValue" in DropDownListFor Html helper
提问by Dino
As the question says: How to set selectedValue in DropDownListFor Html helper?
正如问题所说:如何在 DropDownListFor Html helper 中设置 selectedValue?
Tried most of the other solutions but none worked that's why I am opening a new question.
尝试了大多数其他解决方案,但没有一个奏效,这就是我提出一个新问题的原因。
Nothing of these helped:
这些都没有帮助:
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2), htmlAttributes: new { @class = "form-control" })
//Not working with or without cast
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Single(x => x.Id == 2)), htmlAttributes: new { @class = "form-control" })
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", (ProjectName.Models.TipDepozita)Model.TipoviDepozita.Where(x => x.Id == 2).FirstOrDefault()), htmlAttributes: new { @class = "form-control" })
@Html.DropDownListFor(m => m.TipPopustaId, new SelectList(Model.TipoviDepozita, "Id", "Naziv", new SelectListItem() { Value="2", Selected=true}), htmlAttributes: new { @class = "form-control" })
I would like to avoid manual creation of SelectListItems or a ViewModel just for the list if possible.
如果可能,我想避免仅为列表手动创建 SelectListItems 或 ViewModel。
回答by
When you use the DropDownListFor()(or DropDownList()) method to bind to a model property, its the value of the property that sets the selected option.
当您使用DropDownListFor()(or DropDownList()) 方法绑定到模型属性时,它是设置所选选项的属性值。
Internally, the methods generate their own IEnumerable<SelectListItem>and set the Selectedproperty based on the value of the property, and therefore setting the Selectedproperty in your code is ignored. The only time its respected is when you do not bind to a model property, for example using
在内部,这些方法生成自己的IEnumerable<SelectListItem>并根据Selected属性的值设置属性,因此Selected在代码中设置属性会被忽略。它唯一尊重的时间是当您不绑定到模型属性时,例如使用
@Html.DropDownList("NotAModelProperty", new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2))
Note your can inspect the source code, in particular the SelectInternal()and GetSelectListWithDefaultValue()methods to see how it works in detail.
请注意,您可以检查源代码,尤其是SelectInternal()和GetSelectListWithDefaultValue()方法,以了解其详细工作原理。
To display the selected option when the view is first rendered, set the value of the property in the GET method before you pass the model to the view
要在第一次呈现视图时显示选定的选项,请在将模型传递给视图之前在 GET 方法中设置属性的值
I also recommend your view model contains a property IEnumerable<SelectListItem> TipoviDepozitaand that you generate the SelectListin the controller
我还建议您的视图模型包含一个属性,IEnumerable<SelectListItem> TipoviDepozita并SelectList在控制器中生成
var model = new YourModel()
{
TipoviDepozita = new SelectList(yourCollection, "Id", "Naziv"),
TipPopustaId = 2 // set the selected option
}
return View(model);
so the view becomes
所以视图变成
@Html.DropDownListFor(m => m.TipPopustaId, Model.TipoviDepozita, new { @class = "form-control" })
回答by p.sturge
Make Sure that your return Selection Value is a String and not and int when you declare it in your model.
当您在模型中声明它时,请确保您的返回选择值是一个字符串而不是整数。
Example:
例子:
public class MyModel
{
public string TipPopustaId { get; set; }
}
回答by user10155578
public static class EnumHelper
{
public static SelectList EnumToSelectList<TEnum>(this Type enumType, object selectedValue)
{
return new SelectList(Enum.GetValues(enumType).Cast<TEnum>().ToList().ToDictionary(n=> n), "Key", "Value", selectedValue);
}
}
And in your View:
在您的视图中:
@Html.DropDownListFor(model => model.Role, EnumHelper.EnumToSelectList<Role>(typeof(Role), Model.Role), new { htmlAttributes = new { @class = "padding_right" } })
@Html.ValidationMessageFor(model => model.Role, "", new { @class = "text-danger" })
Instead of EnumToList use any Other List and select Key and Value of your Listtype Properties
而不是 EnumToList 使用任何其他列表并选择您的 Listtype 属性的键和值

