简体   繁体   English

级联下拉MVC3返回空的下拉字段

[英]Cascading Dropdown MVC3 returning empty dropdown fields

I am attempting to create a cascading dropdown with MVC3. 我正在尝试使用MVC3创建级联下拉列表。 The parent dropdown is called "Category", when the user selects a Category, a child dropdown is then populated with a list of pictures that belong to that Category. 父下拉列表称为“类别”,当用户选择类别时,子下拉列表中将包含属于该类别的图片列表。 I've got some code in place right now, and I am able to call the controller from the View when the user selects a category. 我现在有一些代码,当用户选择一个类别时,我就可以从“视图”中调用控制器。 Here is my code: 这是我的代码:

Controller: 控制器:

    public ActionResult Pictures(int catId)
    {
        var k = ((List<Picture>) ViewBag.AllPictures)
            .FindAll(x => x.CategoryId == catId)
            .Select(x => new
                {
                    Value = x.PictureId,
                    Text = x.Title
                });

        return Json(k, JsonRequestBehavior.AllowGet);
    }

View: 视图:

        <div class="editor-field">
            @Html.DropDownListFor(model => model.Picture.PictureId, Enumerable.Empty<SelectListItem>(),  new { @id = "pictureFilter" })
            @Html.ValidationMessageFor(model => model.Picture.PictureId)
        </div>

Javascript: Javascript:

<script type="text/javascript">
    $('#ddlFilter').on("change", function() {
        var selectedCat = $(this).val();
        $.getJSON("/StoreManager/Pictures", { catId: selectedCat }, function(pictures) {
            var picturesSelect = $('#pictureFilter');
            picturesSelect.empty();
            $.each(pictures, function(index, picture) {
                picturesSelect.append($('<option/>', {
                    value: picture.val,
                    text: picture.text
                }));
            });
        });  
    });
</script>

When I take a look at variable 'k', that my controller is returning. 当我查看变量“ k”时,我的控制器正在返回。 It does contain all the correct collection items for the pictures, with their respective 'value' and 'text' fields assigned. 它确实包含所有正确的图片收集项目,并为其分配了相应的“值”和“文本”字段。 When it returns the JSON back to the View, it creates a dropdown menu with the exact number of fields that should be there, but they all contain empty data. 当将JSON返回到View时,它将创建一个下拉菜单,其中包含应该在其中的字段的确切数量,但是它们都包含空数据。 When I inspect the element in Chrome, here is the HTML afterwards: 当我检查Chrome中的元素时,以下是HTML:

<option><option/>
<option><option/>
<option><option/>
<option><option/>

All help is appreciated. 感谢所有帮助。 Any further code requested will be linked to in pastebin posts. 请求的任何其他代码都将链接到pastebin帖子中。

You have return JSON then you need to used same variables as you send from Pictures controller. 您已经返回JSON,那么您需要使用与从Pictures控制器发送的变量相同的变量。 try this: 尝试这个:

<script type="text/javascript">
    $('#ddlFilter').on("change", function() {
        var selectedCat = $(this).val();
        $.getJSON("/StoreManager/Pictures", { catId: selectedCat }, function(pictures) {
            var picturesSelect = $('#pictureFilter');
            picturesSelect.empty();
            $.each(pictures, function(index, picture) {
                picturesSelect.append($('<option/>', {
                    value: picture.Value,
                    text: picture.Text
                }));
            });
        });  
    });
</script>

or you can also check the response variable get from your Action method by using firebug console tab. 或者,您也可以使用Firebug控制台标签检查从Action方法获取的响应变量。

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

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