MVC DataAnnotations验证用于复选框组 [英] MVC DataAnnotations validation For group of checkboxes

查看:78
本文介绍了MVC DataAnnotations验证用于复选框组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下树名复选框



第一柜台

第二柜台

第三个柜台




这样我可以选择多个复选框,但我需要验证用户应该选择至少一个复选框



这是我的模特



I have tree check boxes like below names

First Counter
Second Counter
Third Counter


so that i can choose more than one check box ,but i need to validate user should select atleast one checkbox

This is My Model

class AddDetails
{
 public Boolean FirstCounter{ get; set; }
 public Boolean SecondCounter{ get; set; }
 public Boolean ThirdCounter{ get; set; }
  
}



这是我的观点


This is my view

@Html.CheckBoxFor(mmm => mmm.FirstCounter) <label>First</label>
@Html.CheckBoxFor(mmm => mmm.SecondCounter) <label>Second</label>
@Html.CheckBoxFor(mmm => mmm.ThirdCounter) <label>Third</label>



我正在使用 @ Html.validatemessagefor()来显示错误消息



因此,如果用户未选择上述任何一个复选框,则应使用验证消息向用户显示警告消息


am using @Html.validatemessagefor() for error message purpose

so if the user not selected any one of the above check boxes, alert message should shown to user using validation message

推荐答案

<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js">

</script>
<script type="text/javascript">
    function CheckBox() {

        if (


(#chk1)。get(0).checked ||
("#chk1").get(0).checked ||


(#chk2 )。G et(0).checked ||
("#chk2").get(0).checked ||


这篇关于MVC DataAnnotations验证用于复选框组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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