2

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.

1
  • 1
    This should function window.location="Controller/Action?value="+yourValue Commented Feb 17, 2012 at 11:00

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Doh ! Instead of value, I should use id as string? Am I right ?
michael - yes that would do the trick!! tho i had assumed you had an edge case for using the 'value' paramater, hence the 'new' route :)
+1. @MichaelSwan, if you don't want to edit the route; you can pass parameter using name-value pair window.location="Controller/Action?value="+yourValue
sandeep - yup that would work equally well (i'll add that to the answer to to give the 3 alternatives

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.