简体   繁体   English

URL.Action传递null,需要整数

[英]URL.Action passing null, need integer

I'm in the process of building a simple website using MVC 4 and razor. 我正在使用MVC 4和剃须刀构建一个简单的网站。 I'm having trouble receiving an int from an url.action that is being filled on the fly. 我无法从正在填充的url.action接收整数。 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. 我已经调试,发现id确实有一个值(id恰好是1),但是当它传递时,它以某种方式变为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. 我真的不知道幕后发生了什么事情,导致它变为null,我需要帮助。 I'm using just the default routing provided by Visual Studio right now: 我现在仅使用Visual Studio提供的默认路由:

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

Your passing a route value named id 您传递的路由值名为id

new { id = e.ID }

so change the method parameter name to match it 因此更改方法参数名称以使其匹配

public ActionResult EventDetails(int? id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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