问题 通过参数mvc重定向到Action


我想重定向到其他Controller中的操作但它不起作用 这是我在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 Index(Int32)'中   '... Controllers.ProductImageManagerController'。   可选参数必须是引用类型,可空类型或be   声明为可选参数。参数名称:参数


1321
2017-11-12 13:04


起源

由于参数顺序无效,RedirectToImages无法重定向到ProductImageManager / Index - LINQ2Vodka
@jim我只有一个参数id,那么参数命令在这里意味着什么? - Mohammadreza
需要 "Index", "ProductImageManager" 代替 "ProductImageManager","Index" - LINQ2Vodka
如何 public static void RegisterRoutes(RouteCollection routes) 方法体看起来像?将其添加到消息中 - LINQ2Vodka
@jim重定向Url我得到/ ProductManager / Index而不是/ ProductManager / Index / 1,为什么我的参数不发送? - Mohammadreza


答案:


对于同一控制器中的重定向,您无需指定控制器。不确定你是否需要让参数为nullable来进行这种重定向,或者如果我们将它作为可空的,因为我们需要另外一次,但这是来自一个工作项目:

[HttpGet]
public ActionResult EditRole(int? selectedRoleId)
{
    AddEditRoleViewModel role = _userService.GetAllRoles(selectedRoleId);
    return View(role);
}

[HttpPost]
public ActionResult EditRoleSave(AddEditRoleViewModel role)
{
    _userService.SaveRole(role);
    return RedirectToAction("EditRole", new { selectedRoleId = role.Id });
}

编辑

调用不同的控制器,您可能需要使用a RouteValueDictionary

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

您提供的示例应该适用于您的 RouteConfig 是为它配置的,所以你应该检查它,以便你正确设置它。检查 这个stackoverflow问题 和答案了解更多信息。

编辑2

根据@Mohammadreza的评论,错误发生在 RouteConfig。 要让应用程序处理带有id的URL,您需要确保为其配置了Route。你这样做 RouteConfig.cs 位于 App_Start 夹。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //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 }
    );
}

15
2017-11-12 13:33



亲爱的@Binke他们不一样。 productManger和productImageManger。我的错误在于在routConfig中进行配置。我在问题的评论中提到它。任何方式tnx为您的帮助:) - Mohammadreza
您可以直接使用RedirectToAction。不需要路由值字典。 - vohrahul


这应该工作!

[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>

-1
2018-06-07 20:00



不,问题在于路由ProductImageManagerController索引操作。 - Mohammadreza
您可以将app.routes代码粘贴到此处作为问题的一部分吗?因为它有效。不确定为什么你downvoted! - vohrahul


尝试这个,

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

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


-2
2017-11-12 13:10



在哪里 SiteID 来自? - Mike Perrenoud


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

这是一个无效的参数顺序,应该是一个动作第一个

确保您的路由表正确无误


-3
2017-11-12 13:06



这与OP完全相同。 - Mike Perrenoud
这没有用。修复你的答案,我摆脱了我的downvote。 - Mike Perrenoud
@MichaelPerrenoud你贬低你的业力:) - LINQ2Vodka
大声笑,你搞砸了我。 - Mike Perrenoud
亲爱的@jim没有参数重定向到ProductImageManager / Index。我很困惑。 - Mohammadreza