ASP.NET MVC - passing parameters to the controller


I have a controller with an action method as follows:

public class InventoryController : Controller
    public ActionResult ViewStockNext(int firstItem)
        // Do some stuff

And when I run it I get an error stating:

The parameters dictionary does not contain a valid value of type 'System.Int32' for parameter 'firstItem'. To make a parameter optional its type should either be a reference type or a Nullable type.

I had it working at one point and I decided to try the function without parameters. Finding out that the controller was not persistant I put the parameter back in, now it refuses to recognise the parameter when I call the method.

I'm using this url syntax to call the action:


Any ideas why I would get this error and what I need to do to fix it?

I've tried adding another method that takes an integer to the class it it also fails with the same reason. I've tried adding one that takes a string, and the string is set to null. I've tried adding one without parameters and that works fine, but of course it won't suit my needs.

7/13/2012 7:13:56 AM

Accepted Answer

Your routing needs to be set up along the lines of {controller}/{action}/{firstItem}. If you left the routing as the default {controller}/{action}/{id} in your global.asax.cs file, then you will need to pass in id.

    new { controller = "Inventory", action = "ListAll", firstItem = "" }

... or something close to that.

10/1/2008 1:46:32 AM

you can change firstItem to id and it will work

you can change the routing on global.asax (i do not recommed that)

and, can't believe no one mentioned this, you can call :


In a @Url.Action would be :

@Url.Action("ViewStockNext", "Inventory", new {firstItem=11});

depending on the type of what you are doing, the last will be more suitable. Also you should consider not doing ViewStockNext action and instead a ViewStock action with index. (my 2cents)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow