最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Updating a Drop down list with Jquery from JSON - Stack Overflow

programmeradmin2浏览0评论

I have a list that I need to fill using a JSON collection of object. 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 e up with for my javascript, gathered from various sources:

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 have a list that I need to fill using a JSON collection of object. 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 e up with for my javascript, gathered from various sources:

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?

Share Improve this question asked Sep 28, 2010 at 1:55 Grahame AGrahame A 3,95312 gold badges47 silver badges70 bronze badges 1
  • What does your response from the server look like? – Nick Craver Commented Sep 28, 2010 at 1:59
Add a ment  | 

2 Answers 2

Reset to default 6

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? if so, do it like this, use .append()

selectList.append(option);

with that, I'm still assuming 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);


added notes for .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:

  • DOM select object's .add()
  • jQuery object's .add()

JQuery fails silently if it doesn't like the JSON data ing back from the server. Point your browser at /Admin/Product/GetProductCategories and look at the result. Make sure it doesn't have any html tags and make sure there are double quotes around the key names.

发布评论

评论列表(0)

  1. 暂无评论