ASP.NET MVC级联DropDownLists Javascript问题

时间:2012-07-19 02:59:12

标签: javascript asp.net-mvc asp.net-mvc-3 razor

在回顾了Cascading DropDownLists的许多教程和各种方法之后,我决定为我的View创建一个ViewModel,然后根据这篇文章填充我的DropDownLists: MVC3 AJAX Cascading DropDownLists

这里的目标是最基本的,并且在许多教程中都有所涉及,但我仍然无法让它完全正确...根据州下拉列表的值填充城市下拉列表。

修改

自从发布此请求以获得帮助后,我发现了Firebug(是的,这就是我对进行任何编程的新方式),并且我能够确定我成功调用了我的控制器并提取了必要的数据。我相信问题是我的JavaScript的后半部分将数据返回到我的View。

以下是我的观点:

    <label>STATE HERE:</label>
    @Html.DropDownListFor(x => x.States, Model.States, new { @class = "chzn-select", id = "stateID" })
    <br /><br />
    <label>CITY HERE:</label>
    @Html.DropDownListFor(x => x.Cities, Enumerable.Empty<SelectListItem>(), new { id = "cityID" })

以下是我的视图中的JavaScript,并且一旦我得到它,我就不能正确处理我的结果:

$(function () {
    $("#stateID").change(function () {
        var stateId = $(this).val();
        // and send it as AJAX request to the newly created action 
        $.ajax({
            url: '@Url.Action("GetCities")',
            type: 'GET',
            data: { Id: stateId },
            cache: 'false',

            success: function (result) {
                var citySelect = $('#cityID');
                $(citySelect).empty();

                // when the AJAX succeeds refresh the ddl container with

                $.each(result, function (result) {
                    $(citySelect)
                    .append($('<option/>', { value: this.simpleCityID })
                    .text(this.cityFull));

                });
            },
            error: function (result) {
                alert('An Error has occurred');
            }
        });
    });
});

这是我的控制器,由JavaScript调用:

public JsonResult GetCities(int Id)
    {
        return Json(GetCitySelectList(Id), JsonRequestBehavior.AllowGet);
    }

    private SelectList GetCitySelectList(int Id)
    {
        var cities = simpleDB.simpleCity.Where(x => x.simpleStateId == Id).ToList();

        SelectList result = new SelectList(cities, "simpleCityId", "cityFull");
        return result;
    }

以下是来自Firbug的结果,它告诉我我正在构建并获取没有问题的数据,只是没有正确填充我的DropDownList:

[{"Selected":false,"Text":"Carmel","Value":"IN001"},{"Selected":false,"Text":"Fishers","Value":"IN002"}]

如果有人对JavaScript无法填充dropdrown的原因有任何建议,请发表评论,谢谢!

2 个答案:

答案 0 :(得分:0)

我已经做了好几次这样的事情:

创建部分到popolate下拉列表。将其命名为DropDownList并放入视图的Shared文件夹

@model SelectList     
@Html.DropDownList("wahtever", Model)

您的创建视图应该是这样的(跳过不相关的部分)

<script type="text/javascript">
    $(function() {
        $("#StateId").change(function() {
            loadLevelTwo(this);
        });

        loadLevelTwo($("#StateId"));
    });

    function loadLevelTwo(selectList) {
        var selectedId = $(selectList).val();

        $.ajax({
            url: "@Url.Action("GetCities")",
            type: "GET",
            data: {stateId: selectedId},
            success: function (data) {
                $("#CityId").html($(data).html());
            },
            error: function (result) {
                alert("error occured");
            }
        });
    }
</script>

@Html.DropDownList("StateId")

<select id="CityId" name="CityId"></select>

请注意Select的{​​{1}}空CityId项以及loadLevelTwo

document.ready来电

你的控制器应该像:

public ActionResult Create()
{
    ViewBag.StateId = new SelectList(GetAllCities(), "Id", "Name");
    return View();
}

public ActionResult GetCities(int stateId) {
    SelectList model = new SelectList(GetCitiesOfState(stateId), "Id", "Name");
    return PartialView("DropDownList", model);
}

答案 1 :(得分:0)

感谢您的协助,

事实证明,在我的下面的JavaScript中,我试图直接引用与我的数据模型相关联的 simpleCityID cityFull 字段:

$.each(result, function (result) {
                $(citySelect)
                .append($('<option/>', { value: this.simpleCityID })
                .text(this.cityFull));

相反,我需要保持它的通用性和内联JavaScript标准,引用文字

$.each(modelData, function (index, itemData) {
                select.append($('<option/>', {
                    value: itemData.Value,
                    text: itemData.Text