【问题标题】:Redirect to Action by parameter mvc通过参数mvc重定向到Action
【发布时间】:2013-11-12 13:04:28
【问题描述】:

我想重定向到其他控制器中的操作,但它不起作用 这是我在 ProductManagerController 中的代码:

[HttpPost]
public ActionResult RedirectToImages(int id)
{
    return RedirectToAction("Index","ProductImageManeger", new   { id=id   });
}

这在我的 ProductImageManagerController 中:

[HttpGet]
public ViewResult Index(int id)
{
    return View("Index",_db.ProductImages.Where(rs=>rs.ProductId == id).ToList());
}

它很好地重定向到 ProductImageManager/Index 没有参数(没有错误),但是使用上面的代码我得到了这个:

参数字典包含一个空条目 方法的不可为空类型“System.Int32”的参数“ID” 'System.Web.Mvc.ViewResult 索引(Int32)' 在 '...Controllers.ProductImageManagerController'。 可选参数必须是引用类型、可空类型或 声明为可选参数。参数名称:参数

【问题讨论】:

  • RedirectToImages 无法重定向到 ProductImageManager/Index,因为参数顺序无效
  • @jim 我只有一个参数id,那么这里的参数顺序是什么意思?
  • 需要"Index", "ProductImageManager" 而不是"ProductImageManager","Index"
  • public static void RegisterRoutes(RouteCollection routes) 方法体是什么样的?将其添加到消息中
  • @jim 在重定向网址中我得到 /ProductManager/Index 而不是 /ProductManager/Index/1 ,为什么我的参数没有发送?

标签: c# asp.net-mvc redirect asp.net-mvc-5


【解决方案1】:

这个错误非常不具描述性,但这里的关键是 'ID' 是大写的。这表明路由没有正确设置。要让应用程序处理带有 id 的 URL,您需要确保至少为其配置了一个路由。您可以在 App_Start 文件夹中的 RouteConfig.cs 中执行此操作。最常见的是将id作为可选参数添加到默认路由中。

public static void RegisterRoutes(RouteCollection routes)
{
    //adding the {id} and setting is as optional so that you do not need to use it for every action
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

现在您应该能够按照您设置的方式重定向到您的控制器。

[HttpPost]
public ActionResult RedirectToImages(int id)
{
    return RedirectToAction("Index","ProductImageManager", new { id });

    //if the action is in the same controller, you can omit the controller:
    //RedirectToAction("Index", new { id });
}

在一两次之前,我通过正常的重定向遇到了一些问题,不得不通过传递一个 RouteValueDictionary 来解决这个问题。更多信息RedirectToAction with parameter

return RedirectToAction("Index", new RouteValueDictionary( 
    new { controller = "ProductImageManager", action = "Index", id = id } ) 
);

如果你得到一个非常相似的错误但是是小写的'id',这通常是因为路由需要一个没有提供的id参数(调用一个没有id/ProductImageManager/Index的路由) .请参阅this so question 了解更多信息。

【讨论】:

  • 亲爱的@Binke 他们不一样。产品管理器和产品图像管理器。我的错误是在 routConfig 中进行配置。我在问题的评论中提到了它。任何方式 tnx 为您提供帮助:)
  • 您可以简单地使用 RedirectToAction。不需要路由值字典。
【解决方案2】:

这应该可以!

[HttpPost]
public ActionResult RedirectToImages(int id)
{
    return RedirectToAction("Index", "ProductImageManeger", new  { id = id });
}

[HttpGet]
public ViewResult Index(int id)
{
    return View(_db.ProductImages.Where(rs => rs.ProductId == id).ToList());
}

请注意,如果您返回的视图与操作实现的视图相同,则不必传递视图名称。

您的视图应该像这样继承模型:

@model <Your class name>

然后您可以在视图中访问您的模型:

@Model.<property_name>

【讨论】:

  • 不,问题在于路由 ProductImageManagerController 索引操作。
  • 您能否在此处粘贴您的 app.routes 代码作为问题的一部分?因为它有效。不知道你为什么投反对票!
【解决方案3】:

试试这个,

return RedirectToAction("ActionEventName", "Controller", new { ID = model.ID, SiteID = model.SiteID });

在这里我提到你也传递了多个值或模型。 这就是我在这里提到这一点的原因。

【讨论】:

  • SiteID 是从哪里来的?
【解决方案4】:
return RedirectToAction("ProductImageManager","Index", new   { id=id   });

这是一个无效的参数顺序,应该是一个动作优先
AND
确保您的路由表正确

【讨论】:

  • 这并没有什么好处。修正你的答案,我就不再投反对票了。
  • 大声笑,你把我搞砸了。
  • 亲爱的@jim 它没有参数重定向到 ProductImageManager/Index 。我很困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
相关资源
最近更新 更多