简体   繁体   中英

URL.Action passing null, need integer

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

new { id = e.ID }

so change the method parameter name to match it

public ActionResult EventDetails(int? id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM