2

I am quite new to MVC and Jquery and I am trying to dynamically load data from a database to populate a dropdown list. Although the data is returned from the controller, it is not displayed in the dropdown. What is wrong here?

Here is my controller code:

public JsonResult GetReasonforLeave(int typecode)
        {
List<LeaveReason> reason_list = itlbg1.LeaveReasons.Where(f => f.LeaveType_typeCode == typecode).ToList(); 
return Json(reason_list, JsonRequestBehavior.AllowGet);

        }

Here's the jquery:

function getreason(selecteditem) {

        sel_val = selecteditem.value;

        $.getJSON("/Leave/GetReasonforLeave", { typecode: sel_val })
            .done(function (data) {
            var options = $("#reason");
            $.each(data, function (item) {

                options.append($("<option />").val(item.reasonID).text(item.description));
            });


        });
    }
5
  • Try $.each(data, function (index, item) { Commented Jun 22, 2015 at 3:41
  • 1
    Also, unless LeaveReason contains only 2 properties (reasonID and description) you are sending unnecessary data across the wire - consider sending just the 2 properties you need - var data = itlbg1.LeaveReasons.Where(f => f.LeaveType_typeCode == typecode).Select(r => new { value = r.reasonID, text = description }); Commented Jun 22, 2015 at 3:45
  • Thanks for that improvement on the controller, I tried it but alert(data); did not return any data. What is meant here by the index variable? Commented Jun 22, 2015 at 3:46
  • Refer documentation - the callback expects 2 arguments, Integer indexInArray and Object value Commented Jun 22, 2015 at 3:49
  • Thank you for the informative response Stephen. I'll try to figure the error. Commented Jun 22, 2015 at 3:57

1 Answer 1

1

Did the changes Stephen requested and it worked. Here's the working code FYI. Thanks Stephen. Jquery:

function getreason(selecteditem) {

        sel_val = selecteditem.value;

        $.getJSON("/Leave/GetReasonforLeave", { typecode: sel_val })
            .done(function (data) {
            alert(data);
            var options = $("#reason_dd");
            options.empty();
            $.each(data, function (index, item) {

                options.append($("<option />").val(item.value).text(item.text));

            });


        });
    }

Changed controller to:

public JsonResult GetReasonforLeave(int typecode)
        {

            var reason_list = itlbg1.LeaveReasons.Where(f => f.LeaveType_typeCode == typecode).Select(r => new { value = r.reasonID, text = r.description }); 
            return Json(reason_list, JsonRequestBehavior.AllowGet);

        }
Sign up to request clarification or add additional context in comments.

4 Comments

You probably also want to include options.empty() to remove any existing options before you populate the new ones.
Updated the answer. You guys are just awesome.
Actually you have it wrong - your clearing the options in each iteration - options.empty(); needs to go before the $.each() statement :)
Yes. I was wondering why only the first option was loading. All fixed now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.