Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC下拉列表,onchange_C#_Asp.net Mvc 3 - Fatal编程技术网

C# MVC下拉列表,onchange

C# MVC下拉列表,onchange,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我在html中有两个Dropdownlistfor,Dropdownlistfor Country和Dropdownlistfor state。我使用MVC,我想像这样输出,如果我改变所选国家,所选国家的状态将填入dropdownlistfor状态。我从数据库中获取国家和州的数据,州表中有一个countryId字段。谢谢你的帮助 下面是Html中的示例代码 <div class="fieldBlock"> <div>

我在html中有两个Dropdownlistfor,Dropdownlistfor Country和Dropdownlistfor state。我使用MVC,我想像这样输出,如果我改变所选国家,所选国家的状态将填入dropdownlistfor状态。我从数据库中获取国家和州的数据,州表中有一个countryId字段。谢谢你的帮助

下面是Html中的示例代码

            <div class="fieldBlock">
                <div><label>Country</label>&nbsp;*</div>
                <div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div>
                @Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation)
                <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
            </div>

            <div class="fieldBlock">
                <div>@Html.LabelFor(model => model.Employee.StateId, "State/Province")&nbsp;*</div>
                <div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305" })</div>
                <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
            </div>

国家*
@Html.DropDownListFor(model=>model.Country.Id,model.CountrySelectList,新的{@class=“width305”,@Id=“ddlCountry”})
@Html.HiddenFor(model=>model.Country.Name)@Html.HiddenFor(model=>model.Country.缩写)
@Html.ValidationMessageFor(model=>model.Employee.StateId)
@Html.LabelFor(model=>model.Employee.StateId,“州/省”)*
@Html.DropDownListFor(model=>model.Employee.StateId,model.CountryStateProvinceSelectList,新的{@class=“width305”})
@Html.ValidationMessageFor(model=>model.Employee.StateId)
试试这个

鉴于

<script type="text/javascript">
     $(function() {
            $("#ddlCountry").change(function() {
                var selectedItem = $(this).val();
                var ddlStates = $("#ddlState");
                var statesProgress = $("#states-loading-progress");
                statesProgress.show();
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: "@(Url.Action("SomeAction", "SomeController"))",
                    data: { "countryId": selectedItem},
                    success: function (data) {
                        ddlStates.html('');
                        $.each(data, function(id, option) {
                            ddlStates.append($('<option></option>').val(option.id).html(option.name));
                        });

                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to retrieve states.');

                    }  
                });
            });
        });
    </script>


<div class="fieldBlock">
    <div><label>Country</label>&nbsp;*</div>
    <div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div>
    @Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation)
    <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>

<div class="fieldBlock">
    <div>@Html.LabelFor(model => model.Employee.StateId, "State/Province")&nbsp;*</div>
    <div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305", @id = "ddlState" })</div>
    <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div>
</div>

注意:检查
Id
是否正确绑定到下拉列表,并且我已经编写了用于获取状态的伪代码。

可能的重复,如果您知道,你实际上问了一个问题,而不是简单地陈述一大堆事情,期望我们猜测你想要什么。我认为你需要在答案的控制器部分进行编辑。操作的返回类型必须是
JsonResult
public ActionResult SomeAction(string countryId)
{
     //perform your action here
     var states = _stateProvinceService.GetStateProvincesByCountryId(country.Id).ToList();
     var result = (from s in states
                   select new { id = s.Id, name = s.GetLocalized(x => x.Name) }
                  ).ToList();

   return Json(result, JsonRequestBehavior.AllowGet);
}