Snake Eyes
Snake Eyes

Reputation: 16764

Send a string parameter to a controller (MVC3) from javascript (no ajax)

I have this simple javascript code:

var value = "I love programming";
window.location="Controller/Action/"+value;

In controller page, I have this:

public ActionResult(string value) {
   // do something
}

The problem is value parameter from controller. This is always null.

Why ?

Is the parameter type is restricted to int? (without using ajax)

I can send an int to controller and it process the information correctly. But not string.

Upvotes: 2

Views: 2007

Answers (1)

jim tollan
jim tollan

Reputation: 22485

You may need to set up a route in global.asax to handle the string parameter, i.e:

RouteTable.Routes.MapRoute(
    "ValueRoute",
    "{controller}/{action}/{value}",
    new
    {
        controller = "Yourcontroller",
        action = "Youraction",
        value = UrlParameter.Optional
    }
);

this is all top of the head stuff, so it may not happen for you.

[update 1] as you say in the comment below, changing the parameter name from value=>id should resolve the problem without recourse to addtional routes.

[update 2] - you could also, as per sandeep's comment, opt for the name-value pair on the url, i.e. window.location="Controller/Action?value="+yourValue

Upvotes: 3

Related Questions