I'm in the process of building a simple website using MVC 4 and razor. I'm having trouble receiving an int from an url.action that is being filled on the fly. I have debugged and found that the id does have a value (the id happens to just be 1) but when it is passed it somehow becomes null.
The View portion:
@if (Model.Any())
{
foreach (var e in Model)
{
<a href="@Url.Action("EventDetails", "Event", new{id = e.ID})">
<div id="eventBox">
<!-- stuff !-->
</div>
</a>
}
}
And the controller:
public ActionResult EventDetails(int? eventID)
{
if (eventID != null)
{
//stuff
}
}
I really don't know what is happening behind the scenes to cause it to change to null and I need help. I'm using just the default routing provided by Visual Studio right now:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Your passing a route value named id
so change the method parameter name to match it