0

I have a jquery $.getJson call to a controller action that returns a json.

The action accepts 3 parameters:

public async Task<ActionResult> MyAction(string id, string name, string age)
{
    .... code here     
}

and in JavaScript

$.getJson('@Url.Action("MyAction", "MyController", new { @id= Model.Id, @name=Model.Name, @age=Model.Age })')

The problem is that in the action only the Id and Name values are provided the age is null. The age value is ther. If I just display the age on the page

@Model.Age

the values is shown ... somehow is not set to the action.

The route looks like this:

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

Basically only the first 2 parameters are sent to action. The third one is null. I have a feeling is a route issues here, but cannot figure it out.

2 Answers 2

2

You are sending a JSON object to the controller, why not send 3 parameters? Otherwise your controller action really needs something like this to match your request:

public class DataClass{
   public string id;
   public string name;
   public string age;
}

Change your controller:

public async Task<ActionResult> MyAction(DataClass data)
{
   .... code here     
}
Sign up to request clarification or add additional context in comments.

10 Comments

then it's getting set as null
why don't you show your js Model class? Also place a debugger; line before you send the data to verify.
There is actually no data that is sent but just a get issues to that url with the parameters. The @Url.Action helper formats the url but the parameters are received from the model. Actually I checked it with Fiddler and the request (url) is formatted properly. The problem is in the MVC side.
Here is the url from fiddler: https://localhost:44314/MyController/MyAction/2f9d59d1-92ae-4028-b3d7-e08c2ebf0123?name=Jhon&amp;age=23&_=1437254972485
As you can see all parameters are there: id, name and age. on the mvc side age only is null
|
0

I actually solved this by adding a new route to my RouteConfig.cs class:

Now it looks like this. Notice the new name and age parameters:

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


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

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

Comments

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.