对于布尔MVC DropDownListFor默认值 [英] MVC DropDownListFor default value for bool
本文介绍了对于布尔MVC DropDownListFor默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有真/假值的dropdownlistfor。我想要的默认值是真实的。现在它是假的,因为布尔的默认值是假的。此下拉有时候会在JavaScript动态创建的,因此被传递之前,我不能在模型中设置的值。如何将默认设置为True在CHTML文件?
感谢您。
// ---------- CSHTML文件---------------
...
< TD类=valuebool>
@ Html.DropDownListFor(M = GT; m.IsBoolFilterCondition,
新的SelectList(
新[]
{
//下面两行默认为false
//新的{值=真,文本=真},
//新的{值=假,文本=FALSE}, //接下来的两行也默认为false
//新SelectListItem {值=真,文本=真,选择= TRUE},
//新SelectListItem {值=假,文本=FALSE,选择= FALSE}, //下面两行也默认为false
新{值=真,文本=真,选择= TRUE},
新{值=假,文本=FALSE,选择= FALSE},
},
值,
文本
)
新{@类=valuebool}
)
...// -------模型类------------------------
公共类FilterCondition中
{
...
公共BOOL IsBoolFilterCondition {搞定;组; }
...
}
// ---------------浏览器源----------------------- 真正
假
解决方案
@ Html.DropDownListFor(M = GT; m.IsBoolFilterCondition,
新的SelectList(
新[]
{
新SelectListItem {值=1,文本=真,选择= TRUE},
新SelectListItem {值=0,文本=FALSE},
},
值,
文本
)
新{@类=valuebool}
)
I have a dropdownlistfor with True/False values. I want the default value to be true. Right now it's false because the default of a bool is false. This dropdown sometimes gets created dynamically in javascript so I can't set the value in the model before it gets passed. How do I set the default to True in the chtml file?
Thank you.
//---------- 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; }
...
}
//--------------- Browser Source ----------------------- True False
解决方案
@Html.DropDownListFor(m => m.IsBoolFilterCondition,
new SelectList(
new[]
{
new SelectListItem { Value = "1", Text = "True" ,Selected = true},
new SelectListItem { Value = "0", Text = "False" },
},
"Value",
"Text"
),
new { @class="valuebool"}
)
这篇关于对于布尔MVC DropDownListFor默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文