简体   繁体   中英

Chosen library disables validation of DropDownList in ASP.NET MVC

In my ASP.NET MVC project the javascript Chosen library will disable the validation for the DropDownList. I referenced below validation scripts in layout page:

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

and the part of my view is as below:

<div class="col-md-10" align="right" style="margin-top: 30px">
   @Html.DropDownList("ProvinceId", null, "- استان را انتخاب نمایید- ", new { required = "true", type = "Text", @class = "form-control" })
   @Html.ValidationMessageFor(model => model.ProvinceId, "", new { @class = "text-danger" })
</div>
<div class="col-md-10" align="right" style="margin-top: 30px" id="divcities">
   @Html.DropDownList("CityId", Enumerable.Empty<SelectListItem>(), "- شهر را انتخاب نمایید -", new { type = "Text", @class = "form-control" })
   @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
</div>

and some part of my jquery code in this view is as below:

@section Scripts{
     <script>

        $('form').validate({

            rules: {
                ProvinceId: {
                    required: true,
                },
                CityId: {
                    required: true,
                }
            },
            messages: {
                ProvinceId: {
                    required: "استان را انتخاب نمایید",
                },
                CityId: {
                    required: "شهر را انتخاب نمایید",
                },
            }
        });

    </script>
    <script>
        $(function() {
            $("#ProvinceId, #CityId").chosen();
        })
    </script>
    }

I must say that all of other validation will work but the validations for this two fields do not work. Any help will be appreciated!

I solved this problem by putting ignore: [ ] , at the end of below script:

   <script>

    $('form').validate({

        rules: {
            ProvinceId: {
                required: true,
            },
            CityId: {
                required: true,
            }
        },
        messages: {
            ProvinceId: {
                required: "استان را انتخاب نمایید",
            },
            CityId: {
                required: "شهر را انتخاب نمایید",
            },
        },
        ignore: [],
    });

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM