2

I'm receiving the following error that my default route parameters are null. I've used this same code on a Controller Action that didn't have any parameters in the URL and it worked fine. I know that my custom route is being called but I don't understand why startIndex and pageSize are showing up null in the action.

Error:

The parameters dictionary contains a null entry for parameter 'startIndex' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ViewVcByStatus(System.String, Int32, Int32)' in 'AEO.WorkOrder.WebUI.Controllers.VendorComplianceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

Controller:

public ActionResult ViewVcByStatus(string status, int startIndex, int pageSize) { ... }

Route:

routes.MapRoute("ViewVcByStatus", "ViewVcByStatus/{status}",
  new
  {
    controller = "VendorCompliance",
    action = "ViewVcByStatus",
    startIndex = 0,
    pageSize = WebConfigurationManager.AppSettings["PageSize"],
   });

Link:

<a href="VendorCompliance/ViewVcByStatus?status=PROCESSED">

Also tried this link which produces the same error:

<a href="VendorCompliance/ViewVcByStatus/PROCESSED">
2
  • Do you have other routes that may be matching? Also that url doesn't match the route you have shown here. Commented Jul 5, 2016 at 19:36
  • The only other route is the default route. I also tried <a href="VendorCompliance/ViewVcByStatus/PROCESSED"> and received the same results. Commented Jul 5, 2016 at 19:37

2 Answers 2

3

Try this.

public ActionResult ViewVcByStatus(string status, int? pageSize, int?startIndex)
    {
        return View();
    }

Route.config

routes.MapRoute(
            name: "ViewVcByStatus",
            url: "ViewVcByStatus/{status}",
            defaults: new { controller = "VendorCompliance", action = "ViewVcByStatus", startIndex = UrlParameter.Optional, pageSize = UrlParameter.Optional });

optional parameters should be declared optional in routeconfig and mark them int? in your action method, This will do the work for you. Hope this helps.This solution will work with your url pattern in your question "http://localhost:53290/VendorCompliance/ViewVcByStatus?status=PROCESSED".

Sign up to request clarification or add additional context in comments.

6 Comments

This is the best practice to mark the parameters nullable to avoid any null entry error. and if you want to pass them with url you can VendorCompliance/ViewVcByStatus?status=PROCESSED&startIndex=10&pageSize=1. Moreover you can assign the default value to the parameters as well. There are many ways, It basically depends on your requirement. Hope I could enplane.
The routing part is entirely correct, I should´ve pointed that out but missed it. Just noticed you edited your ActionResult to be correct too :)
yes, you are right. I have edited my action result to avoid the unnecessary confusion. :)
This is not correct. You cannot have 2 parameters in the route definition marked as UrlParameter.Optional. Only the last one can be optional.
Thanks for commenting, I don't think that only one optional parameter is allowed. You can have multiple optional parameter. Moreover it works fine. I have tested it. If you have any proof, please share it. Also check the link here tutlane.com/tutorial/aspnet-mvc/….
|
1

Send the startIndex and pageSize with the link(I hardcoded it, use parameters instead), your actionresult is expecting all parameters that the link needs to provide, and the MapRoute will probably fall through to default Route because it can´t match it with any other route matching the one parameter you provided

<a href="VendorCompliance/ViewVcByStatus?status=PROCESSED&startIndex=0&pageSize=0">

2 Comments

This worked. Isn't the whole point of specifying the default parameters in the route itself to avoid having to specify them in the link?
Just the opposite. You create a custom Route so that you can find a way around the default behaviour of MVC. But do not use Route lightly, they are there as a way for harder cases when most else fails. Try using the normal MVC patterns as often as possible, by using controllers in the logic and scripts or razor(e.g) possibilities in the Views. This will also make the code much easier to read for other developers. Many don´t understand custom routing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.