zoukankan      html  css  js  c++  java
  • bool?

    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,但验证也未通过,实际接收的值为“”值。

  • 相关阅读:
    固定思维的可怕(转)
    Javascript模块化编程:require.js的用法
    js中将字符串转为JSON的三种方式
    cf 55D 数位dp 好题
    fzu 2113 数位dp
    uestc 250 数位dp(水)
    hdu 3652数位dp
    数位dp 3943 二分法
    hdu 3943 经典数位dp好题
    hdu 4871 树的分治+最短路记录路径
  • 原文地址:https://www.cnblogs.com/dotnetmvc/p/3796913.html
Copyright © 2011-2022 走看看