Trying to get started with MS’s implementation of MVC and I’m already in a jam..
I have a have a route like so:
routes.MapRoute( "Custom", // Route name
"RightHere/{entryDate}", // URL with parameters
new { controller = "RightHere", action = "GetDate" } // Parameter defaults
);
And the corresponding controller action (RightHereController) looks like so:
public ActionResult GetDate(DateTime date)
{
try
{
ViewData["Date"] = date;
return View();
}
catch
{
return View();
}
}
The view:
<h2>RightHere</h2>
<p><%= Html.Encode(ViewData["Date"]) %></p>
Simple enough.. But get the orange screen of death when using URL /RightHere/GetDate/1-1-2009
:
The parameters dictionary contains a null entry for parameter 'date' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult GetDate(System.DateTime)'
Need you more tenured Ms MVC peps tell me what I’m missing 🙂
You actually have two problems. The first problem is that the URL you mentioned above doesn’t match the route you created. The second problem lies with your route and your action not quite matching.
You have this for the URL in your question:
/RightHere/GetDate/1-1-2009
It needs to be this:
/RightHere/1-1-2009
If you want it to be the latter, then you need to change your route to this:
You have this for your action:
It needs to be this, to match your route:
Other than that, you’re good to go. Just make sure you have the route at the top, above the default route. I can recommend one other thing that would help is by adding a constraint to your route, for example:
This makes sure that the entry date passed in, is in the correct format.