tags:

views:

699

answers:

2

Hi Guys,

I am trying to implement an edit page in order administrator to modify data in database.Unfortunately I am encountering an error.

The code below:

public ViewResult Edit(int productId) { // Do something here }

but I am getting this error:

"The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Edit(Int32)' in 'WebUI.Controllers.AdminController'. To make a parameter optional its type should be either a reference type or a Nullable type.

Parameter name: parameters"

I changed my route in Global.asax.cs like this:

 routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit",  productId= "" }

);

but still I am getting the error

Thanks for your help!

Naim

+2  A: 

That empty string for productId (in your default route) will get parsed to a null entry by the Framework, and since int does not allow null...you're getting the error.

Change:

public ViewResult Edit(int productId)

to

public ViewResult Edit(int? productId)

if you want to allow for the caller to not have to pass in a product id, which is what it looks like what you want to do based on the way your route is configured.

You could also re-configure your default route to pass in some known default for when no productId is supplied:

routes.MapRoute( 
    "Admin", 
    "Admin/{action}/{ productId}", 
    new { controller = "Admin", action = "Edit",  productId= -1 } 
Justin Niessner
A: 

the standard "int" class (int32) does not accept null values, and it will fail a cast from an empty string to int in that case and try and assign null to it.

I would probably take a look at what you're trying to accomplish -- if you're trying to force the administrator to have a productID present for them to edit that record in the database, I would think about putting that into the Request object or some other method that will give some more versatility.

Jason M