复选框和下拉列表的自定义验证

时间:2014-01-29 16:12:52

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 data-annotations

我在我的某个表单上使用DataAnnotations进行验证。字符串类型的验证是可以的。它适用于必需和错误消息。但是在验证复选框时我遇到了问题。我有某种规范/规则,如。

我有一组包含4个复选框的复选框。我想验证它,以便至少选择一个复选框,这是必须的。 但就性别而言,应该标记性别,但一次只应检查一个性别。  我怎样才能在Mvc中实现这一目标。

我发现另外一个难以验证的字段,那就是与ViewBag捆绑在一起的Dropdownlist。如果Dropdown与Viewbag捆绑在一起,如何使用dataAnnotations验证下拉列表。我正在使用一个隐藏字段来发布Dropdownlist.Below的值是我的代码::

我真的很难找到解决方案。

View Page
@{
    ViewBag.Title = "Home Page";
}
@model MvcValidation.Models.Model
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>


<style type="text/css">
    #DataContent
    {
        width: 100%;
        height: 1000px;
    }

    .Contentdiv
    {
        float: left;
        width: 100%;
        height: 100%;
    }

    .HeadingDiv
    {
        float: left;
        width: 100%;
        padding-bottom: 2%;
    }

    .CommonDiv
    {
        float: left;
        width: 100%;
    }

    .LeftDiv
    {
        float: left;
        width: 50%;
    }
</style>

@using (Ajax.BeginForm("Register", "Home", new AjaxOptions { HttpMethod = "POST", OnSuccess = "updateSuccess" }))
{

    <div id="DataContent">
        <div class="Contentdiv">
            <div class="HeadingDiv">Register here</div>
            <div class="CommonDiv">
                <div class="LeftDiv">
                    Name
                </div>
                @Html.HiddenFor(m => m.ddlValue)
                @Html.HiddenFor(m => m.Gender_Check)
                @Html.HiddenFor(m => m.Mood_Check)
                <div class="LeftDiv">
                    @Html.TextBoxFor(m => m.Name)
                    @Html.ValidationMessageFor(m => m.Name)
                </div>
            </div>

            <div class="CommonDiv">
                <div class="LeftDiv">
                    Email
                </div>
                <div class="LeftDiv">
                    @Html.TextBoxFor(m => m.Email)
                    @Html.ValidationMessageFor(m => m.Email)
                </div>
            </div>

            <div class="CommonDiv">
                <div class="LeftDiv">
                    Father Name
                </div>
                <div class="LeftDiv">
                    @Html.TextBoxFor(m => m.FatherName)
                    @Html.ValidationMessageFor(m => m.FatherName)
                </div>
            </div>

            <div class="CommonDiv">
                <div class="LeftDiv">
                    Mother Name
                </div>
                <div class="LeftDiv">
                    @Html.TextBoxFor(m => m.MotherName)
                    @Html.ValidationMessageFor(m => m.MotherName)
                </div>
            </div>
            <div class="CommonDiv">
                <div class="LeftDiv">
                    Country
                </div>
                <div class="LeftDiv">
                    @Html.DropDownList("myCountriesDDL", (SelectList)ViewBag.Countries, "Select")
                </div>
            </div>

            <div class="CommonDiv">
                <div class="LeftDiv">
                    Gender
                </div>
                <div class="LeftDiv">
                    Male @Html.CheckBoxFor(m => m.Male)
                    Female @Html.CheckBoxFor(m => m.Female)
                </div>
            </div>
            <div class="CommonDiv">
                <div class="LeftDiv">
                    Mood
                </div>
                <div class="LeftDiv">
                    None @Html.CheckBoxFor(m => m.None)
                    Mild @Html.CheckBoxFor(m => m.Mild)
                    Moderate @Html.CheckBoxFor(m => m.Moderate)
                    Severe @Html.CheckBoxFor(m => m.Severe)
                    Flexible @Html.CheckBoxFor(m => m.Flexible)
                    @Html.ValidationMessageFor(m => m.Mood_Check)
                </div>
            </div>

            <div class="CommonDiv">
                <div class="LeftDiv">
                </div>
                <div class="LeftDiv">
                    <input type="submit" value="Submit" onclick="AssignValues();" />
                </div>
            </div>
        </div>
    </div>
}


<script type="text/javascript">
    function updateSuccess(Json) {
        alert(Json.Msg);
    }
    function AssignValues() {
    var country = $("#myCountriesDDL").val();
    $("#ddlValue").val(country);

    if ($("#None").checked == true || $("#Mild").checked == true || $("#Moderate").checked == true
        || $("#Severe").checked == true || $("#Flexible").checked == true) {
        $("#Mood_Check").val("Checked");
    }
}
</script>


Model//
public class Model
    {
        public bool None { get; set; }
        public bool Mild { get; set; }
        public bool Severe { get; set; }
        public bool Moderate { get; set; }
        public bool Flexible { get; set; }
        public bool Male { get; set; }
        public bool Female { get; set; }
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }
        [Required(ErrorMessage = "Address is required")]
        public string Address { get; set; }
        [Required(ErrorMessage = "Email is required")]
        public string Email { get; set; }
        [Required(ErrorMessage = "Father name is required")]
        public string FatherName { get; set; }
        [Required(ErrorMessage = "Mother name is required")]
        public string MotherName { get; set; }
        public string Country { get; set; }
        public string ddlValue { get; set; }
       [Required(ErrorMessage = "Mood is required")]
        public string Mood_Check { get; set; }
       [Required(ErrorMessage = "Gender is required")]
       public string Gender_Check { get; set; }
    }


Controller

 public ActionResult Register(Model ObjModel)
        {
            return Json(new { Msg = "Hello" });
        } 

0 个答案:

没有答案