简体   繁体   English

级联下拉验证在MVC Razor中失败

[英]Cascading Dropdown Validation is failing in MVC Razor

Problem Statement: 问题陈述:

I have created a cascading dropdown for Country and State using Jquery and Json,everything is working fine.I'm able to bind data for Country and data for State on change event of Country.Only problem what i'm facing is cascading validation for Country and State. 我已经使用Jquery和Json为Country和State创建了一个级联下拉列表,一切工作正常。我能够在Country的更改事件中绑定Country的数据和State的数据。我所面临的唯一问题是级联验证国家和州。

On opening the View iff I click create button without selecting/entering any values for the controls it is showing validation message for all.If i select Country and click create, it is not showing validation message for state even if no value is selected for State. 在打开View iff时,我单击创建按钮时未选择/输入任何控件的值,但显示所有消息的验证消息。如果我选​​择``国家/地区''并单击创建,则即使未为State选择任何值,也不会显示状态验证消息。

What i'm doing wrong?? 我做错了什么? I think i'm doing something wrong in Script part of the view !!! 我认为我在脚本的视图部分做错了!

Please see the attached images for more details: 请参阅所附的图片以获取更多详细信息:

Image 1 : When I click create button it is showing validations for all controls. 图片1: 当我单击创建按钮时,它显示了对所有控件的验证。

图片1

Image 2 : After selecting Country drop-down if i click create button,validation for state is not appearing why?? 图片2: 如果选择“创建”按钮,则选择“国家/地区”下拉菜单后,为什么没有显示国家验证?

图片2

View Code: 查看代码:

<div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.cityModel.CountryID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.cityModel.CountryID, new SelectList(Model.ddlCountryStateCity.ddlCountry, "Value", "Text"), "Select Country", new { id="CountryID",onchange="GetddlState()",@class = "form-control"})
                @Html.ValidationMessageFor(model => model.cityModel.CountryID)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.cityModel.StateID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.cityModel.StateID, new SelectList(Model.ddlCountryStateCity.ddlState, "Value", "Text"), "Select State", new { id="StateID",@class = "form-control" })
                @Html.ValidationMessageFor(model => model.cityModel.StateID)
            </div>
        </div>

        <div class="form-group">
              @Html.LabelFor(model => model.cityModel.CityID, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                 @Html.TextBoxFor(model => model.cityModel.CityID, new { @class = "form-control" })
                 @Html.ValidationMessageFor(model => model.cityModel.CityID)
            </div>
        </div>

         <div class="form-group">
               @Html.LabelFor(model => model.cityModel.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                 @Html.TextBoxFor(model => model.cityModel.Name, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.cityModel.Name)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>

Script Code: 脚本代码:

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
    function GetddlState() {
        $("#StateID").empty();
        $("#StateID").append("<option value='0'>Select State</option>");
        var countryID = $('#CountryID').val();
        var Url = "@Url.Content("~/MasterConfigGeneral/GetddlState")";
        $.ajax({
            url: Url,
            dataType: 'json',
            data: { CountryID: countryID },
            success: function (data) {
                $("#StateID").empty();
                $("#StateID").append("<option value='0'>Select State</option>");
                $.each(data, function (index, optiondata) {
                    $("#StateID").append("<option value='" + optiondata.Value + "'>" + optiondata.Text + "</option>");
                });
            }
        });
    }

 </script>
}

Try This: 尝试这个:

 @section Scripts {
   @Scripts.Render("~/bundles/jqueryval")
      <script type="text/javascript">
         function GetddlState() {
         var countryID = $('#CountryID').val();
         var Url = "@Url.Content("~/MasterConfigGeneral/GetddlState")";
         $.ajax({
           url: Url,
           dataType: 'json',
           data: { CountryID: countryID },
           success: function (data) {
            $("#StateID").empty();
            $("#StateID").append("<option value='' selected='selected'>Select State</option>");
            $.each(data, function (index, optiondata) {
            $("#StateID").append("<option value='" + optiondata.Value + "'>" + optiondata.Text + "</option>");
            });
        }
    });
 }

</script>
}
             type: "POST",
            data: JSON.stringify({"id": idd}),
            contentType: "application/json; charset=utf-8",

Adding these three parameters under $.ajax will solve the problem $.ajax下添加这三个参数可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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