【发布时间】:2014-06-29 22:16:47
【问题描述】:
我有一个带有 True/False 值的下拉列表。我希望默认值为 true。现在它是假的,因为 bool 的默认值是假的。此下拉列表有时会在 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 },
// The following two lines also default to false
new { Value = "true", Text = "True", Selected = true },
new { Value = "false", Text = "False", Selected = false },
},
"Value",
"Text"
),
new { @class="valuebool"}
)
...
//------- Model Class ------------------------
public class FilterCondition
{
...
public bool IsBoolFilterCondition { get; set; }
...
}
//--------------- 浏览器源 ----------- 真的 错误的
【问题讨论】:
标签: asp.net-mvc boolean dropdownlistfor