正如问题所说:如何在DropDownListFor Html助手中设置selectedValue?
尝试了大多数其他解决方案,但没有工作,这就是为什么我要开一个新问题.
这些都没有帮助:
@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 = …Run Code Online (Sandbox Code Playgroud) 我有一个带有true / false值的dropdownlistfor。我希望默认值为true。现在它是假的,因为布尔值的默认设置是假。该下拉列表有时会在javascript中动态创建,因此我无法在传递值之前在模型中设置值。如何在chtml文件中将默认值设置为True?
谢谢。
//---------- cshtml file ---------------
...
<td class="valuebool">
@Html.DropDownListFor(m => m.IsBoolFilterCondition,
new SelectList(
new[]
{
// The following two lines defaulted to false
//new { Value = "true", Text = "True" },
//new { Value = "false", Text = "False" },
// The next two lines also defaulted to false
//new SelectListItem { Value = "true", Text = "True", Selected = true },
//new SelectListItem { Value = "false", Text = "False", Selected = false },
// …Run Code Online (Sandbox Code Playgroud)