简体   繁体   English

使用JSON从JSON更新下拉列表

[英]Updating a Drop down list with Jquery from JSON

I have a list that I need to fill using a JSON collection of object. 我有一个列表,需要使用JSON对象集合进行填充。 Here is what my action is returning: 这是我的操作返回的内容:

public ActionResult GetProductCategories()
        {
            var categories = _entities.ProductCategories.ToList();

            var result = new List<SelectListItem>();

            foreach (var category in categories)
            {
                result.Add(new SelectListItem {Text = category.CategoryName, Value = category.CategoryId.ToString()});
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

This is what I've come up with for my javascript, gathered from various sources: 这是我从各种来源收集来的javascript脚本:

function loadCategories() {
            $.getJSON("/Admin/Product/GetProductCategories", null, function (data) {
                var selectList = $("#subCategories");

                $.each(data, function(index, optionData)
                {
                    var option = new Option(optionData.Text, optionData.Value);
                    selectList.add(option, null);
                });                
            });
        }

But it doesn't appear to be working, and isn't giving any errors. 但是它似乎没有用,并且没有给出任何错误。 Whats the best practice for this sort of thing? 这种事情的最佳实践是什么?

I was wondering what are you trying to achieve in this next lines of codes, 我想知道您在接下来的几行代码中想要实现什么,

var option = new Option(optionData.Text, optionData.Value);
selectList.add(option, null);

are you trying to create an option then add it on select ? 您是否要创建一个option然后将其添加到select if so, do it like this, use .append() 如果是这样,请使用.append()

selectList.append(option);

with that, I'm still assuming new Option(optionData.Text, optionData.Value); 这样,我仍然假设new Option(optionData.Text, optionData.Value); is creating a new option , in jQuery it would be like this var option = $('<option>').text(optionData.Text).val(optionData.Value); 正在创建一个新option ,在jQuery中将类似于此var option = $('<option>').text(optionData.Text).val(optionData.Value);


added notes for .add() , .add()添加了注释,

var selectList = $("#subCategories"); // creates jQuery object selectList.
selectList.add(option, null); // selectList is jQuery object,
                              // so when you call .add(), you're calling jQuery's `add()`.

a workaround is this, 解决方法是

selectList[0].add(option, null); // you can use [0] to convert it into DOM object.

difference between: 之间的区别:

JQuery fails silently if it doesn't like the JSON data coming back from the server. 如果JQuery不喜欢从服务器返回的JSON数据,则会以静默方式失败。 Point your browser at /Admin/Product/GetProductCategories and look at the result. 将浏览器指向/ Admin / Product / GetProductCategories,然后查看结果。 Make sure it doesn't have any html tags and make sure there are double quotes around the key names. 确保它没有任何html标记,并确保键名周围有双引号。

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

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