MVC5 - 如何在 DropDownListFor Html 助手中设置 "selectedValue"
MVC5 - How to set "selectedValue" in DropDownListFor Html helper
正如问题所说:
如何在 DropDownListFor Html 助手中设置 selectedValue?
尝试了大多数其他解决方案,但 none 有效,这就是为什么我提出一个新问题。
这些都没有帮助:
@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" })
如果可能,我希望避免仅为列表手动创建 SelectListItems 或 ViewModel。
当您使用 DropDownListFor()
(或 DropDownList()
)方法绑定到模型 属性 时,它是设置所选选项的 属性 的值。
在内部,这些方法生成自己的 IEnumerable<SelectListItem>
并根据 属性 的值设置 Selected
属性,因此设置 Selected
] 属性 在你的代码中被忽略。唯一受尊重的情况是您不绑定到模型 属性,例如使用
@Html.DropDownList("NotAModelProperty", new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2))
请注意,您可以检查 source code,特别是 SelectInternal()
和 GetSelectListWithDefaultValue()
方法以详细了解其工作原理。
要在首次呈现视图时显示所选选项,请在将模型传递给视图之前在 GET 方法中设置 属性 的值
我还建议您的视图模型包含一个 属性 IEnumerable<SelectListItem> TipoviDepozita
并且您在控制器中生成 SelectList
var model = new YourModel()
{
TipoviDepozita = new SelectList(yourCollection, "Id", "Naziv"),
TipPopustaId = 2 // set the selected option
}
return View(model);
所以视图变成
@Html.DropDownListFor(m => m.TipPopustaId, Model.TipoviDepozita, new { @class = "form-control" })
确保您的 return 选择值是字符串,而不是当您在模型中声明它时的整数。
示例:
public class MyModel
{
public string TipPopustaId { get; set; }
}
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);
}
}
在您看来:
@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" })
不要使用 EnumToList,而是使用任何其他列表和 select 列表类型属性的键和值
我注意到没有面向剃刀的方法,我在下面添加
var prices = from P in Model[idx].prices.Values
where !P.Key.ToLower().Contains("san")
select new SelectListItem()
{
Text = P.Key + " Month " + (Convert.ToDecimal(P.Value) + ((Convert.ToDecimal(P.Value) / 100) * 20)).ToString("0.##") + " $",
Value = P.Key
};
prices.ToList()[0].Selected = true;
@Html.DropDownListFor(model => prices.ToList()[0], prices)
正如问题所说: 如何在 DropDownListFor Html 助手中设置 selectedValue?
尝试了大多数其他解决方案,但 none 有效,这就是为什么我提出一个新问题。
这些都没有帮助:
@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" })
如果可能,我希望避免仅为列表手动创建 SelectListItems 或 ViewModel。
当您使用 DropDownListFor()
(或 DropDownList()
)方法绑定到模型 属性 时,它是设置所选选项的 属性 的值。
在内部,这些方法生成自己的 IEnumerable<SelectListItem>
并根据 属性 的值设置 Selected
属性,因此设置 Selected
] 属性 在你的代码中被忽略。唯一受尊重的情况是您不绑定到模型 属性,例如使用
@Html.DropDownList("NotAModelProperty", new SelectList(Model.TipoviDepozita, "Id", "Naziv", 2))
请注意,您可以检查 source code,特别是 SelectInternal()
和 GetSelectListWithDefaultValue()
方法以详细了解其工作原理。
要在首次呈现视图时显示所选选项,请在将模型传递给视图之前在 GET 方法中设置 属性 的值
我还建议您的视图模型包含一个 属性 IEnumerable<SelectListItem> TipoviDepozita
并且您在控制器中生成 SelectList
var model = new YourModel()
{
TipoviDepozita = new SelectList(yourCollection, "Id", "Naziv"),
TipPopustaId = 2 // set the selected option
}
return View(model);
所以视图变成
@Html.DropDownListFor(m => m.TipPopustaId, Model.TipoviDepozita, new { @class = "form-control" })
确保您的 return 选择值是字符串,而不是当您在模型中声明它时的整数。
示例:
public class MyModel
{
public string TipPopustaId { get; set; }
}
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);
}
}
在您看来:
@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" })
不要使用 EnumToList,而是使用任何其他列表和 select 列表类型属性的键和值
我注意到没有面向剃刀的方法,我在下面添加
var prices = from P in Model[idx].prices.Values
where !P.Key.ToLower().Contains("san")
select new SelectListItem()
{
Text = P.Key + " Month " + (Convert.ToDecimal(P.Value) + ((Convert.ToDecimal(P.Value) / 100) * 20)).ToString("0.##") + " $",
Value = P.Key
};
prices.ToList()[0].Selected = true;
@Html.DropDownListFor(model => prices.ToList()[0], prices)