views:

342

answers:

2

I have a JSON request, but it seems that it is not hitting the controller. Here's the jQuery code:

$("#ddlAdminLogsSelectLog").change(function() {
        globalLogSelection = $("#ddlAdminLogsSelectLog").val();

        alert(globalLogSelection);

        $.getJSON("/Administrative/AdminLogsChangeLogSelection", { NewSelection: globalLogSelection }, function(data) {
            if (data.Message == "Success") {
                globalCurrentPage = 1;

            } else if (data.Message == "Error") {
                //Do Something
            }
        });
    });

The alert is there to show me if it actually fired the change event, which it does.

Heres the method in the controller:

public ActionResult AdminLogsChangeLogSelection(String NewSelection)
    {
        String sMessage = String.Empty;
        StringBuilder sbDataReturn = new StringBuilder();

        try
        {
            if (NewSelection.Equals("Application Log"))
            {
                int i = 0;
            }
            else if (NewSelection.Equals("Email Log"))
            {
                int l = 0;
            }
        }
        catch (Exception e)
        {
            //Do Something
            sMessage = "Error";
        }

        return Json(new { Message = sMessage, DataReturn = sbDataReturn.ToString() }, JsonRequestBehavior.AllowGet);
    }

I have a bunch of Json requests in my application, and it seems to only happen in this area. This is a separate area (I have 6 "areas" in the app, 5 of which work fine with JSON requests). This controller is named "AdministrativeController", if that matters.

Does anything jump out anyone as being incorrect or why the request would not pass to the server side?

+1  A: 

Look at the GET in Firebug or Fiddler.

Either:

  • There is no GET, in which case your browser cached the results from last time (cough, IE, cough); change the cache policy on the response.
  • There is a GET, but it doesn't match your route; fix the routing or the JavaScript, as appropriate.
Craig Stuntz
Hahaha...yeah...I like the IE comment there! Definitely tried it...see my "solution" for what we found!
SlackerCoder
A: 

As it turns out, if the Area name and Controller name are the same, it looks like MVC gets a little confused. Im not sure if this is a bug on my side, or something witH MVC, but when I remove the "/" from the name in the Json request (ie. "Administrative/Action" instead of "/Administrative/Action") it works just fine. A colleague was the one to figure this one out for me, he found some forum response on it and showed me what they did. Once I removed the "/" it worked just fine.

SlackerCoder
Without the `/` you have a relative URI. With it, you have an absolute URI. So it's your bug. :)
Craig Stuntz
It's strange though, I have several other areas, and all the Json requests in those have the "/" at the front of the URI and I get no problems. Oh well...this works...so I wont complain :)
SlackerCoder
Right, because you're elsewhere in the site. "Relative" means *relative to the current page.*
Craig Stuntz