Reputation: 4359
I'm getting familiar with MVC 3. I got stuck on the first thing in the tutorial - unbelievable!
This is my Controller-method:
public string Welcome(string name, int numTimes = 1)
{
return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
And the url I'm using:
http://localhost:49770/HelloWorld/Welcome?name=Adam?numTimes=4
Which should produce this:
Hello Adam, NumTimes is: 4
But it actually becomes this:
Hello Adam?numTimes=4, NumTimes is: 1
It does not separate the params! It must be something really simple I've missed!
Upvotes: 1
Views: 396
Reputation: 82297
When forming strings to send parameters, especially if you use JSON to do that with AJAX, in between each field sent must be an &. The & acts similar to a comma in method parameters. Whereas a call from code may be Welcome("Adam",4)
, from a url it would look like /Welcome?name=Adam&numTimes=4
. The ? tells MVC (assuming regular routing) to use the action found, in this case Welcome. Following that are name value pairs, separated by the & symbol.
Upvotes: 1
Reputation: 34359
Use http://localhost:49770/HelloWorld/Welcome?name=Adam&numTimes=4
The series of key/value pairs in a query string are separated with an ampersand.
See http://en.wikipedia.org/wiki/Query_string for details.
Upvotes: 1
Reputation: 7713
You've got 2 question marks (?). change the second one to an ampersand (&)
http://localhost:49770/HelloWorld/Welcome?name=Adam&numTimes=4
Upvotes: 3