mvc html.checkboxfor 不是bool类型,asp.net mvc 3 - Checkbox for nullable boolean - Stack Overflow

Complicating a primitive with hidden fields to clarify whether False or Null is not recommended.

Checkbox isn't what you should be using -- it really only has one state: Checked. Otherwise, it could be anything.

When your database field is a nullable boolean (bool?), the UX should use 3-Radio Buttons, where the first button represents your "Checked", the second button represents "Not Checked" and the third button represents your null, whatever the semantics of null means. You could use a drop down list to save real estate, but the user has to click twice and the choices aren't nearly as instantaneously clear.

1 0 null

True False Not Set

Yes No Undecided

Male Female Unknown

On Off Not Detected

The RadioButtonList, defined as an extension named RadioButtonForSelectList, builds the radio buttons for you, including the selected/checked value, and sets the

so you can use css to make your radio buttons go horizontal (display: inline-block), vertical, or in a table fashion (display: inline-block; width:100px;)

In the model (I'm using string, string for the dictionary definition as a pedagogical example. You can use bool?, string)

public IEnumerable Sexsli { get; set; }

SexDict = new Dictionary()

{

{ "M", "Male"},

{ "F", "Female" },

{ "U", "Undecided" },

};

//Convert the Dictionary Type into a SelectListItem Type

Sexsli = SexDict.Select(k =>

new SelectListItem

{

Selected = (k.Key == "U"),

Text = k.Value,

Value = k.Key.ToString()

});

I am a

@Html.RadioButtonForSelectList(m => m.Sexsli, Model.Sexsli, "Sex")

@Html.ValidationMessageFor(m => m.Sexsli)

public static class HtmlExtensions

{

public static MvcHtmlString RadioButtonForSelectList(

this HtmlHelper htmlHelper,

Expression> expression,

IEnumerable listOfValues,

String rbClassName = "Horizontal")

{

var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

var sb = new StringBuilder();

if (listOfValues != null)

{

// Create a radio button for each item in the list

foreach (SelectListItem item in listOfValues)

{

// Generate an id to be given to the radio button field

var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

// Create and populate a radio button using the existing html helpers

var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));

var radio = String.Empty;

if (item.Selected == true)

{

radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id, @checked = "checked" }).ToHtmlString();

}

else

{

radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();

}// Create the html string to return to client browser

// e.g. Choice 1

sb.AppendFormat("

{0}{1}
", radio, label, rbClassName);

}

}

return MvcHtmlString.Create(sb.ToString());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值