Nate Pet
Nate Pet

Reputation: 46222

MVC optional parameter for method

I want to create an optional parameter for an ActionResult method.

I have the following:

    public ActionResult ViewReq (int id, string Req = null)

When I tried to do the following:

    http://localhost/RepMedia/Controller1/ViewReq?id=34343?Req="34233"

I tried the following but got an error:

An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I am not sure what I am doing wrong.

Upvotes: 2

Views: 1768

Answers (4)

Minras
Minras

Reputation: 4346

http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233

Use a question sign before the first parameter - all others should be split by ampersand.

Upvotes: 3

Christian Horsdal
Christian Horsdal

Reputation: 4932

The problem is with 'id'. The id must be part of the base URI:

http://localhost/RepMedia/Controller1/ViewReq/34343?Req=34233

Upvotes: 3

Oliver Bock
Oliver Bock

Reputation: 5105

public ActionResult ViewReq (int? id, string Req)

http://localhost/RepMedia/Controller1/ViewReq?id=34343&Req=34233

Upvotes: 1

Scott Weaver
Scott Weaver

Reputation: 7361

You don't need make a string parameter optional, as they are reference types whose values will be null anyway if they aren't passed in by MVC. That URL would end up with a non-null "Id", but null "Req".

Upvotes: 0

Related Questions