Sachin Kainth
Sachin Kainth

Reputation: 46740

MVC3 Parameters in Controller Methods

I have a controller called AuctionsController with a method

public ActionResult BidsByAuction(int auctionId, int page = 1)

When I navigate to /Auctions/BidsByAuction/38 I get the following error:

The parameters dictionary contains a null entry for parameter 'auctionId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult BidsByAuction(Int32)' in 'Extranet.Controllers.AuctionsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

What is going wrong here?

Upvotes: 0

Views: 424

Answers (3)

Sachin Kainth
Sachin Kainth

Reputation: 46740

Thanks everyone. School boy error. You were all right, I should have used id as the parameter name in the controller method. That is why I got this error.

Upvotes: 0

Romias
Romias

Reputation: 14133

The "38" is mapped to a the default route last parameter {id}. The second parameter should be optional to not being taking into account if it is not provided in the query string.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Did you create a cutom route for your method that takes an AuctionID as a parameter? If not, then by default, the parameter is called "id".

If you want auctionid, then you need to make a new route that sets the name to auctionid.

Not entirely sure that will fix your problem, since there may be a problem with the optional parameter as well, but fix this one and see what you get.

Upvotes: 6

Related Questions