如何在MVC3剃须刀的复选框上进行验证?

时间:2015-08-02 11:51:16

标签: asp.net-mvc-3

我在mvc3 razor上创建了注册页面。我想在用户通知字段上进行验证。以下是我的代码。

 [Required]
 [Display(Name = "Student Notification ?")]
 [Range(typeof(bool), "true", "true", ErrorMessage = "You gotta tick the box!")]
 public Boolean UserNotification { get; set; }

以下是我的注册页面视图

<div class="editor-label">
        @Html.LabelFor(model => model.UserNotification)
    </div>
    <div class="editor-label">
        @Html.CheckBoxFor(model =>model.UserNotification)
        @Html.ValidationMessageFor(model => model.UserNotification)
    </div>
     <p>
        <input type="submit" value="Register" />
    </p>

所以,当我点击按钮时,那里应该有验证信息..

1 个答案:

答案 0 :(得分:0)

您需要更改属性UserNotification的数据类型。变化:

public Boolean UserNotification { get; set; }

要:

public bool UserNotification { get; set; }

Booleanbool之间存在很大差异。