public class GuestResponse
{
[Required(ErrorMessage = "Please enter your name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your phone number")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please specify whether you'll attend")]
public bool? WillAttend { get; set; }
}

Tip As noted earlier, I used a nullable bool for the WillAttend property. I did this so that I could apply the
Required validation attribute. If I had used a regular bool, the value I received through model binding could be only true
or false, and I wouldn’t be able to tell if the user had selected a value. A nullable bool has three possible values: true,
false, and null. The null value will be used if the user hasn’t selected a value, and this causes the Required attribute
to report a validation error. This is a nice example of how the MVC Framework elegantly blends C# features with HTML and
HTTP.

5.1下测试,如果不定义为 bool? ,如果未选择,绑定后值为false,但验证也未通过,实际接收的值为“”值。

bool?的更多相关文章

  1. iOS关于通知传值Bool类型的注意点

    比如: [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_Name" object ...

  2. Context值和bool开关

      Context值和bool开关的相关内容 Context值分为2种 系统默认的context值 服务的context值 Context值的作用 主要是防止有未知文件进入目录文件之中(如将病毒拷贝到 ...

  3. stm32 usb error : identifier "bool" is undefined

    .\usb\USB\usb_pwr.h(54): error:  #20: identifier "bool" is undefinedusb\USB\usb_pwr.h(54): ...

  4. 2.4嵌套多重if else 的闰年判断以及bool变量的用法

    #include<stdio.h> #include<stdbool.h> int main() { int year; bool leap; //把leap定义为bool , ...

  5. bool型变量下标的时候javascript是不能允许的

    jother编码是我最开始想写的内容,原因有两点:1.原理比较简单,不需要太多关于算法的知识.2.比较有趣,是在对javascript有了很深的理解之后催生的产物.如果你只需要知道jother编码和解 ...

  6. .NET Core中合并Expression<Func<T,bool>>的正确姿势

    这是在昨天的 .NET Core 迁移中遇到的问题,之前在 .NET Framework 中是这样合并 Expression<Func<T,bool>> 的: public s ...

  7. Elasticsearch查询——布尔查询Bool Query

    Elasticsearch在2.x版本的时候把filter查询给摘掉了,因此在query dsl里面已经找不到filter query了.其实es并没有完全抛弃filter query,而是它的设计与 ...

  8. and or bool and a or b 原理解释

    python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值. or也是从左到有计算表达式,返回第一个为真的值. 代码如下: IDLE 1.2.4>&g ...

  9. Predicate<T>与Func<T, bool>泛型委托

    引用别人的: static void Main(string[] args) { List<string> l = new List<string>(); l.Add(&quo ...

  10. Elasticsearch判断多列存在、bool条件组合查询示例

    and符号判断多列存在:{   "filter": {     "and": [       {         "exists": {   ...

随机推荐

  1. .NET_Framework_version_history

    http://en.wikipedia.org/wiki/.NET_Framework_version_history

  2. $.getJSON ashx 跨域

    context.Response.AddHeader("Access-Control-Allow-Origin", "*");

  3. z-index 用法

    现在来说说关于z-index的用法,刚刚在写看页面的时候遇见这样的CSS代码,z-index : 2; 当时还不知道是干嘛用的,也不知道有什么作用,上网查了资料才知道. 几个例子吧,当你在需要把页面中 ...

  4. uses-permission权限汇总

    问登记属性   android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permis ...

  5. 基于EasyUi的快速开发框架

    先看图,下边这个简单的增.删.改.查,如果自己写代码实现,这两个页需要多少行代码? 如果再有类似的增.删.改.查,又需要多少行代码? 我最近搞的这个快速开发框架中,代码行数不超过100. 两页的代码如 ...

  6. 51nod1225 余数之和

    打表可以看出规律.分块求就可以了. #include<cstdio> #include<cstring> #include<cctype> #include< ...

  7. jquery中html()/text()/val()区别

    html就是你可以添加<span></span><li></li>的标记text只能写文本如果写了上面的标记则会以文本形式输出,就是输出标签体的内容va ...

  8. Spark快速入门(1)

    1 安装Spark 首先,到 https://spark.apache.org/downloads.html 选择最新的 Spark 版本和 Hadoop 版本(实际上我们暂时用不上 Hadoop,所 ...

  9. js屏弊错误

    <SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SC ...

  10. 【英语】Bingo口语笔记(41) - 有关爱情的表达