2010-01-27 58 views

回答

2

爲什麼不使用Html.CheckBox()?

+0

從技術上講,您不能使用複選框生成空值。如果情況的語義需要「我不知道」的可能性,那麼你需要另一種方法。但否則這是正確的路要走。或者也許'Html.CheckBox(x => x.Property)'。 – 2010-01-27 16:34:52

+1

@NickLarsen,雖然我同意,但OP特別提到了bools。除非它是可空的(沒有說明),否則這不是問題。 – 2010-01-27 17:22:03

2

使用DropDownListFor幫助程序。傳入您的布爾值和一個包含您想要映射回布爾值的值的選擇列表。

Model.MyBooleanList可能與selectlistitems一個選擇列表的{(「是」,真正的);(「否」,FALSE)} Model.MyBoolean就是要獲得/對你的看法

設置一個布爾值
<%= Html.DropDownListFor(m => m.MyBoolean, Model.MyBooleanList)%> 

心連心

13

這是一個古老的線程,但仍然在一些搜索的頂部。

<%= Html.DropDownListFor(model => Model.MyBooleanProperty,new List<SelectListItem>(){ new SelectListItem(){ Text = "Yes", Value="True"}, new SelectListItem(){ Text = "No", Value="False"}})%> 

您也可以實現自己的HTML助手:我建議使用您的View可爲空的布爾屬性

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); 
    } 

您可以通過使用內置的DropDownListFor HTML輔助做到這一點模型,以便下拉不會默認爲「false」或「true」。如果沒有選項被選中,你可以很容易地使用Required屬性來標記viewmodel。

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)); 
    } 
} 
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)