MVC3:为空的布尔创建复选框 [英] MVC3: Creating checkbox for nullable boolean

查看:138
本文介绍了MVC3:为空的布尔创建复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型有一个具有为可以为空的布尔

My model has a boolean that has to be nullable

public bool? Foo
{
   get;
   set;
}

所以在我的剃须刀CSHTML我有

so in my Razor cshtml I have

@Html.CheckBoxFor(m => m.Foo)

除了不工作。无论是否与(布尔)铸造它。如果我做

except that doesn't work. Neither does casting it with (bool). If I do

@Html.CheckBoxFor(m => m.Foo.Value)

这不会产生一个错误,但发布和富设置为空时,它不绑定到我的模型。请告诉我最好的方式在页面上显示Foo和使其绑定到我的模型上的一个帖子?

that doesn't create an error, but it doesn't bind to my model when posted and foo is set to null. Whats the best way to display Foo on the page and make it bind to my model on a post?

推荐答案

我得到了它与

@Html.EditorFor(model => model.Foo) 

,然后在我的EditorTemplates文件夹制作Boolean.cshtml,坚持

and then making a Boolean.cshtml in my EditorTemplates folder and sticking

@model bool?

@Html.CheckBox("", Model.GetValueOrDefault())

中。

这篇关于MVC3:为空的布尔创建复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆