MVC 4 JsonResult Not Firing?

I have JsonResult all-figured-out using MVC 3.
Now I'm having trouble using MVC 4.
I cannot get a result from my Admin controler, GetUsers action.

Q. Could using JQuery ver 2.0.3 be a problem? (I used ver 1.9.1 in MVC3)

[RouteConfig]
Note: I discovered in MVC3 this is needed to 'fire' the action for debugging purposes.

 public static void RegisterRoutes(RouteCollection routes)
 {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Admin1",
                url: "Admin/GetUsers/{id}",
                defaults: new { controller = "Admin", action = "GetUsers", id = "" });

                routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
}

[Action]
 public class AdminController : Controller
 {
       public JsonResult GetUsers(int id) 
        {
            Array data = myGroups.GetUsersInGroup(id);

            return Json(data, JsonRequestBehavior.AllowGet);
        }
 }

[View]
<script src="~/Scripts/jquery-2.0.3.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () 
    {
        $(document).on("change", '#lbxGroups', function () 
        {
            alert(this.value); //Works!
        
           $("#lbxUsers").empty(); //Works!

            var params = {};
            params.id = this.value;

            $.getJSON("/Admin/GetUsers", $.param(params, true), function (data) {

                  $(data).each(function ()
                  {
                      $("#lbxUsers").append($('<option/>', { value: this.UserId }).html(this.UserName));
                  });
             });
        });
    });

</script>

Open in new window

WorknHardrAsked:
Who is Participating?
 
Craig WagnerConnect With a Mentor Software ArchitectCommented:
I'm not sure why you have that first route in there, it doesn't do anything different than the default route. I'm assuming you've already confirmed that the action method is never being called? I'd try removing that first route, from what I can see it's not necessary anyway.
0
 
WorknHardrAuthor Commented:
I originally used groupId instead of id...

Besides the obvious, do you think the Jquery version can cause issues?
0
 
Hiran DesaiSolution ArchitectCommented:
You need to provide exact problem first.
Which can be answered by following,

1) does your script is working fine?
2) Do ajax request is going to server?
3) Are you able to identify whether your GetUsers method is called?
4) At what level you are trying to access URL from $.getJSON("/Admin/GetUsers".. which is dependent on your current level...say you are at (http://something.com/hereYouare)

Please provide exact information so that one can point out.

After looking at your code, Your MVC code in controller and $.get seems right...

why you have onChange on document?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
WorknHardrAuthor Commented:
Solution: I created a new controller named 'Manager' and cut-n-paste exact code from the non-working 'Admin' controller, did same for View. The new 'Manager & View works!

As a test, I deleted the non-working 'Admin' controller and view, cleaned & rebuild solution, closed & reopened VS2012, then created a new 'Admin' controller and view, it's NOT working either, just as before.

Problem: I don't why JQuery fails to find a url starting with '/Admin/'
0
 
Hiran DesaiSolution ArchitectCommented:
@WorknHardr,

I was expecting answer in line to my questions so that I can start from specific point...

such as 1) if script is not working then "Have you seen your scripts are loaded well? (not having any 404 error"

2) Does your break point hits when you call your ajax function?

do answer such question these will be too helpful.
0
 
WorknHardrAuthor Commented:
The new 'Manager' controller & view use identical code and scripts as before in the 'Admin' controller & view.

1. This time no 404 error!
2. Break Point now 'hit' correctly!
0
 
Hiran DesaiSolution ArchitectCommented:
what's the result at line
return Json(data, JsonRequestBehavior.AllowGet);

Open in new window


Also are you getting any error at controller?

if no then  try with following code..
 $.getJSON("/Admin/GetUsers", $.param(params, true), function (data) {

                  $(data).each(function ()
                  {
                      $("#lbxUsers").append($('<option/>', { value: this.UserId }).html(this.UserName));
                  });
             }).complete(function(result){
alert(result);

});

Open in new window


what results comes up?
0
 
WorknHardrAuthor Commented:
thanks for helping me troubleshoot this weird problem...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.