Avatar of WorknHardr
WorknHardr
 asked on

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

ASP.NETjQuery

Avatar of undefined
Last Comment
WorknHardr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Craig Wagner

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
WorknHardr

ASKER
I originally used groupId instead of id...

Besides the obvious, do you think the Jquery version can cause issues?
Hiran Desai

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?
WorknHardr

ASKER
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/'
Your help has saved me hundreds of hours of internet surfing.
fblack61
Hiran Desai

@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.
WorknHardr

ASKER
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!
Hiran Desai

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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
WorknHardr

ASKER
thanks for helping me troubleshoot this weird problem...