带有可选参数的RedirectToAction?

时间:2014-01-28 07:08:03

标签: asp.net-mvc-3 asp.net-mvc-2

在我的操作中,我需要重定向到Action(我使用aspx视图引擎)。

  [HttpGet]
  public virtual ActionResult Home(Guid id, int? pid)

和此页面的网址如下:

http://localhost:56445/Bid/Home/23b0b096-aea8-46e4-9a7d-a2bd00eaf72e

在我目前的行动中,我有Guid的价值而不是pid。

我正在尝试return RedirectToAction("Home","Bid",new {Guid=23b0b096-aea8-46e4-9a7d-a2bd00eaf72e,pid=0});

但它是错误的:它创建了一个url,其查询方式如下所示:

http://localhost:56445/Bid/Home?Guid=23b0b096-aea8-46e4-9a7d-a2bd00eaf72e&pid=0

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Home(System.Guid, System.Nullable`1[System.Int32])' in 'IntraClockAuction.Web.Controllers.BidController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

你能指导和帮助。

1 个答案:

答案 0 :(得分:2)

您需要传递与您在“操作方法”中接受的名称相同的参数,在您的情况下,id不是Guid

更改

return RedirectToAction("Home","Bid",new {Guid=23b0b096-aea8-46e4-9a7d-a2bd00eaf72e,pid=0});

return RedirectToAction("Home","Bid",new {id="23b0b096-aea8-46e4-9a7d-a2bd00eaf72e",pid=0});