URL.Action传递null,需要整数

时间:2014-11-16 05:35:11

标签: c# asp.net-mvc-4 routes url.action

我正在使用MVC 4和razor构建一个简单的网站。我在从动态填充的url.action接收int时遇到问题。我已经调试并发现id确实有一个值(id恰好是1)但是当它被传递时它会以某种方式变为null。

观看部分:

@if (Model.Any())
{
  foreach (var e in Model)
  {
    <a href="@Url.Action("EventDetails", "Event", new{id = e.ID})">
      <div id="eventBox">
        <!-- stuff !-->
      </div>
    </a>
  }
}

控制器:

public ActionResult EventDetails(int? eventID)
{
     if (eventID != null)
     {
         //stuff
     }
}

我真的不知道幕后发生了什么,导致它变为null,我需要帮助。我现在只使用Visual Studio提供的默认路由:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

1 个答案:

答案 0 :(得分:2)

您传递了名为id

的路线值
new { id = e.ID }

所以更改方法参数名称以匹配它

public ActionResult EventDetails(int? id)