1

So on a change of one drop down list I'm trying to populate a second list.

I have this client side function:

 $('#ProjectReference').change(function() {

       var url = "@Url.Action("GetProjectIterations", "Rally")";
       var data = { selectedProject: $('#ProjectReference').val() };


       $.getJSON(url, data, function(iterations) {
              alert("hello?");
              var items;
              $.each(iterations, function(i, iteration) {
                   items += "<option value='" + iteration.Name + "'>" + iteration.Name + "</option>";
              });
            }
       );

       $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            data: data,
            contentType: 'application/json; charset=utf-8',
            success: function (iterations) {
                        alert("hello?");
                        var items;
                        $.each(iterations, function(i, iteration) {
                            items += "<option value='" + iteration.Name + "'>" + iteration.Name + "</option>";
                        });

                        $("#ProjectIteration").html(items);},
             failure: function () { alert("nope");}
            });
        });

I've tried 2 different formats for calling my controller (so I know I don't need both - I've just included both to highlight what I've tried).

My controller gets called, I am returning data but none of my alerts get fired (and nothing happens to the drop down list I am trying to update). So maybe the way I'm returning my data isn't correct? My select list has a count of 33 so it appears to have data before its returned.

public ActionResult GetProjectIterations(string selectedProject)
{
     var projectIterations = cache.Get("ProjectIterations") as Hashtable;
     var iterations = (projectIterations[selectedProject] as ArrayList).Cast<DynamicJsonObject>().ToList();
     var selectList = iterations.Select(
            iteration => new SelectListItem {Text = iteration["Name"], Value = iteration["Name"]});
     return Json(selectList);
}
Jen
  • 1,964
  • 9
  • 33
  • 59
  • 1
    Have you tried running it through [Fiddler](http://www.fiddler2.com/fiddler2/) or using Javascript debugging with breakpoints (in Chrome, for example) to watch exactly what's happening? You'll be able to watch what you're sending, what you're getting back, and what you're trying to do with it. – Norm MacLennan Feb 04 '13 at 01:18
  • 1
    replace `failure` option in $.ajax with `error` and give a try. Also place a debugger in both `success` and `error` check if it hits either of them – Karthik Chintala Feb 04 '13 at 05:17

1 Answers1

1

Since you're using GET you need to be changing the JsonRequestBehavior on your controller action.

In your example, the code should be:

public ActionResult GetProjectIterations(string selectedProject)
{
     var projectIterations = cache.Get("ProjectIterations") as Hashtable;
     var iterations = (projectIterations[selectedProject] as ArrayList).Cast<DynamicJsonObject>().ToList();
     var selectList = iterations.Select(
            iteration => new SelectListItem {Text = iteration["Name"], Value = iteration["Name"]});
     return Json(selectList, JsonRequestBehavior.AllowGet); //check the overload for Json()
}
da7rutrak
  • 548
  • 3
  • 13
  • Is GET not allowed by default? I figured it was appropriate for this situation - as opposed to POST as I wasn't modifying data. Thanks! – Jen Feb 04 '13 at 22:39
  • 1
    No, and it's a security issue. This question/answer provides some insight: http://stackoverflow.com/questions/8464677/why-is-jsonrequestbehavior-needed – da7rutrak Feb 04 '13 at 23:15