0

I have a piece of code which requests some list of objects using getjson method. Controller action method returns list of objects properly. Following code appends requested data in drop down list, but it is invisible/transparent data means it is appending my data but it is not visible i.e. it white/transparent. Here is the code:

    <script>

        $(document).ready(function () {



            $('#b1').click(function () {

                var userName = "Hello"

                $.getJSON("/classes/getCourseList?username=" + userName, function (data1) {

                    var myOptions =
                        {
                            val1: data1.title

                        };

                    var $mySelect = $('#s1');
                    $.each(myOptions, function (val, text) {
                        $mySelect.append($('<option />',
                            {

                                value: val,
                                text: text
                            }));
                    });



                });

            });

        });
    </script>

Action method:

[AllowAnonymous]
        public JsonResult getCourseList(string userName)
        {

            // Quiz q=new Quiz();
            //q = _db.Quizzes.FirstOrDefault(x => x.QuizName.Equals(userName));
            List<dummyCourses> list = new List<dummyCourses>();

            foreach (Course c in db.Courses) 
            {
                dummyCourses dc = new dummyCourses();
                dc.title = c.title;
                dc.creditHours = c.creditHours;
                dc.instructor = c.instructor;
                dc.code = c.code;
                list.Add(dc);
            }
            return this.Json(list, JsonRequestBehavior.AllowGet);


        }
NN796
  • 1,247
  • 11
  • 31
  • This solutions may help you. http://stackoverflow.com/questions/740195/adding-options-to-a-select-using-jquery-javascript – ddagsan Mar 17 '15 at 09:22
  • What structure did you recive from action? I think it's something like `[ { "title": "Title", ... }, ... ]` I mean array of objects, but you use `data1.title` (it will be `undefined`) in your code. Try `data1[ 0 ].title`. – ostapische Mar 17 '15 at 09:29
  • @ostapische Thanks a lot it was the mistake , you can post it as your answer :) – NN796 Mar 17 '15 at 09:43

1 Answers1

1

You recive structure like:

[ { "title": "Title", ... }, ... ]  

I mean array of objects, but you use data1.title (it will be undefined) in your code.

It must be data1[ 0 ].title, data1[ 1 ].title, etc.

ostapische
  • 1,572
  • 2
  • 12
  • 26