1

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

这里的目标是最基本的,并且在许多教程中都有介绍,但我仍然不能完全正确……根据州下拉菜单的值填充城市下拉菜单。

编辑:

自从发布此帮助请求后,我发现了 Firebug(是的,这就是我做任何类型编程的新方法),并且我能够确定我成功地调用了我的控制器,并提取了必要的数据。我相信问题是我的 JavaScript 的后半部分将数据返回到我的视图。

这是我的观点:

    <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 无法填充下拉菜单的原因有任何建议,请发表评论,谢谢!

4

2 回答 2

0

Thank you for your assistance,

It turns out that in my JavaScript below, I was attempting to directly reference the simpleCityID and cityFull fields associated with my data model:

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

Instead, I needed to keep it generic and inline with JavaScript standards of referencing Value and Text:

$.each(modelData, function (index, itemData) {
                select.append($('<option/>', {
                    value: itemData.Value,
                    text: itemData.Text
于 2012-07-19T20:11:59.707 回答
0

我已经用这样的方法做了好几次:

创建一个部分填充下拉列表。将其命名DropDownList并放入SharedViews 文件夹

@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>

仔细注意 Empty Selectitem forCityIdloadLevelTwoat的调用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);
}
于 2012-07-19T03:58:17.977 回答