可空布尔值的复选框 [英] Checkbox for nullable boolean

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

问题描述

我的模型有一个布尔值,必须为空

My model has a boolean that has to be nullable

public bool? Foo
{
   get;
   set;
}

所以在我的Razor cshtml中,

so in my Razor cshtml I have

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

,除了那不起作用.也不会使用(bool)对其进行强制转换.如果我愿意

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

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

这不会产生错误,但是在发布并且foo设置为null时不会绑定到我的模型.在页面上显示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())

内部.

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

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