kaze
kaze

Reputation: 4359

MVC 3, url parameter does not separate

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

Answers (3)

Travis J
Travis J

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

devdigital
devdigital

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

Al W
Al W

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

Related Questions