是否有用于在下拉列表中显示布尔值的html辅助方法?
答案 0 :(得分:13)
这是一个旧帖子,但仍然是一些搜索的顶部。
您可以使用内置的DropDownListFor HTML Helper来完成此操作:
<%= Html.DropDownListFor(model => Model.MyBooleanProperty,new List<SelectListItem>(){ new SelectListItem(){ Text = "Yes", Value="True"}, new SelectListItem(){ Text = "No", Value="False"}})%>
您还可以实现自己的HTML帮助程序:
public static MvcHtmlString BooleanDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return BooleanDropdownListFor(htmlHelper, expression, null);
}
public static MvcHtmlString BooleanDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string EmptyText)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
bool? value = null;
if (metadata != null && metadata.Model != null)
{
if (metadata.Model is bool)
value = (bool)metadata.Model;
else if (metadata.Model.GetType() == typeof(bool?))
value = (bool?)metadata.Model;
}
List<SelectListItem> items = EmptyText != null ?
new List<SelectListItem>() { new SelectListItem() { Text = EmptyText, Value = "" }, new SelectListItem() { Text = "Yes", Value = "True", Selected = (value.HasValue && value.Value == true) }, new SelectListItem() { Text = "No", Value = "False", Selected = (value.HasValue && value.Value == false) } } :
new List<SelectListItem>() {new SelectListItem() { Text = "Yes", Value = "True", Selected = (value.HasValue && value.Value == true) }, new SelectListItem() { Text = "No", Value = "False", Selected = (value.HasValue && value.Value == false) } };
return htmlHelper.DropDownListFor(expression, items);
}
我建议在View Model上使用可以为空的bool属性,以便下拉列表不会默认为“false”或“true”。您可以使用Required属性轻松标记viewmodel,如果未选择任何选项,则该属性将为handel。
答案 1 :(得分:2)
为什么不使用Html.CheckBox()?
答案 2 :(得分:2)
使用DropDownListFor帮助程序。传入您的布尔值和包含要映射回布尔值的值的选择列表。
Model.MyBooleanList可能是具有selectlistitems的选择列表{(“是”,true);(“否”,false)} Model.MyBoolean只是您想要在视图上获得/设置的bool值
<%= Html.DropDownListFor(m => m.MyBoolean, Model.MyBooleanList)%>
HTH
答案 3 :(得分:0)
我有这个:
public static class BoolUtility
{
public static IEnumerable<SelectListItem> SelectList(string defaultText = null, string defaultTrue = "True", string defaultFalse = "False")
{
var list = new List<SelectListItem>
{
new SelectListItem {Text = defaultTrue, Value = "True"},
new SelectListItem {Text = defaultFalse, Value = "False"}
};
if (defaultText != null)
{
list.Insert(0, new SelectListItem
{
Text = defaultText,
Value = string.Empty
});
}
return list;
}
}
并像这样使用它:
@Html.DropDownListFor(m => m.SomeBoolProperty, BoolUtility.SelectList("All", "Yes", "No"))
它似乎运作良好。非常灵活,因为您可以控制所有标签,以及是否存在“默认”值。
哦,还有一些NUnit单元测试,如果你愿意的话。绝不是全面的,但这并不复杂......
[TestFixture]
class BoolUtilityTests
{
[Test]
public void Parameterless()
{
var actual = BoolUtility.SelectList().ToList();
Assert.That(actual.Count, Is.EqualTo(2));
Assert.That(actual.First().Text, Is.EqualTo("True"));
Assert.That(actual.First().Value, Is.EqualTo("True"));
Assert.That(actual.Last().Text, Is.EqualTo("False"));
Assert.That(actual.Last().Value, Is.EqualTo("False"));
}
[Test]
public void LabelOverrides()
{
var actual = BoolUtility.SelectList(defaultTrue: "Yes", defaultFalse: "No").ToList();
Assert.That(actual.Count, Is.EqualTo(2));
Assert.That(actual.First().Text, Is.EqualTo("Yes"));
Assert.That(actual.First().Value, Is.EqualTo("True"));
Assert.That(actual.Last().Text, Is.EqualTo("No"));
Assert.That(actual.Last().Value, Is.EqualTo("False"));
}
[Test]
public void IncludeDefaultOption()
{
var actual = BoolUtility.SelectList(defaultText: "all").ToList();
Assert.That(actual.Count, Is.EqualTo(3));
Assert.That(actual.First().Text, Is.EqualTo("all"));
Assert.That(actual.First().Value, Is.EqualTo(string.Empty));
}
}
答案 4 :(得分:0)
如果你想捕获YES,NO和NOTSET(没有答案)有点值,我建议你使用这样的下拉列表
Public Class MyModelClass
{
[Display(Name = "Project Based Subsidy?")]
public Nullable<bool> IsSubsidyProjectBased{ get; set; }
public SelectList DropDownItems
{
get
{
List<DropDownItems> ddItem = new List<DropDownItems>();
ddItem.Add(new DropDownItems("--Select--", null));
ddItem.Add(new DropDownItems("Yes", true));
ddItem.Add(new DropDownItems("No", false));
return new SelectList(ddItem, "Value", "Text");
}
}
public class DropDownItems
{
public DropDownItems(string text, bool? value)
{
this.Text = text;
this.Value = value;
}
public string Text { get; set; }
public bool? Value { get; set; }
}
}
然后在您的视图中,您可以轻松拥有
@Html.DropDownListFor(model => model.IsSubsidyProjectBased, Model.DropDownItems)