在[HttpGet]操作中,Id始终为null

时间:2013-10-05 17:48:42

标签: asp.net asp.net-mvc asp.net-mvc-4

我正在重定向到另一个页面,其中Id为参数。我使用RedirectToAction成功传递了ID,但controller action方法不接受ID,并且即使在网址中存在Id时也始终为null提供

重定向到该页面的代码

   return RedirectToAction("myaction", new System.Web.Routing.RouteValueDictionary(
                    new { controller = "mycontroller", action = "myaction", Id = 1 }));

Url看起来像这样

  http://localhost:1234/mycontroller/myaction/1

Action看起来像这个

  [HttpGet]
  public ActionResult myaction(int? Id)
  {
      // ID is null here 
  }

1 个答案:

答案 0 :(得分:1)

尝试一次

return RedirectToAction("myaction", "mycontroller", new {Id = 1} ); 

return RedirectToAction("myaction", "mycontroller", 
       new System.Web.Routing.RouteValueDictionary( new { Id = 1 }));