C# 通过参数 mvc 重定向到 Action
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19929990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect to Action by parameter mvc
提问by Mohammadreza
I want to redirect to an action in other Controller but it doesn't work here's my code in ProductManagerController:
我想重定向到其他控制器中的操作,但它不起作用这是我在 ProductManagerController 中的代码:
[HttpPost]
public ActionResult RedirectToImages(int id)
{
return RedirectToAction("Index","ProductImageManeger", new { id=id });
}
and this in my ProductImageManagerController:
这在我的 ProductImageManagerController 中:
[HttpGet]
public ViewResult Index(int id)
{
return View("Index",_db.ProductImages.Where(rs=>rs.ProductId == id).ToList());
}
It redirect to ProductImageManager/Index without parameter very well(no error)but with above code i get this:
它重定向到没有参数的 ProductImageManager/Index 非常好(没有错误)但使用上面的代码我得到了这个:
The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(Int32)' in '...Controllers.ProductImageManagerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
对于“...Controllers.ProductImageManagerController”中的方法“System.Web.Mvc.ViewResult Index(Int32)”,参数字典包含不可为空类型“System.Int32”的参数“ID”的空条目。可选参数必须是引用类型、可为空类型或声明为可选参数。参数名称:参数
回答by LINQ2Vodka
return RedirectToAction("ProductImageManager","Index", new { id=id });
Here is an invalid parameters order, should be an action first
AND
ensure your routing table is correct
这是一个无效的参数顺序,首先应该是一个操作
并
确保您的路由表正确
回答by Jeet Bhatt
Try this,
尝试这个,
return RedirectToAction("ActionEventName", "Controller", new { ID = model.ID, SiteID = model.SiteID });
Here i mention you are pass multiple values or model also. That's why here i mention that.
在这里我提到你也传递多个值或模型。这就是我在这里提到的原因。
回答by Binke
This error is very non-descriptive but the key here is that 'ID'is in uppercase. This indicates that the route has not been correctly set up. To let the application handle URLs with an id, you need to make sure that there's at least one route configured for it. You do this in the RouteConfig.cslocated in the App_Startfolder. The most common is to add the id as an optional parameter to the default route.
这个错误是非常非描述性的,但这里的关键是“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 }
);
}
Now you should be able to redirect to your controller the way you have set it up.
现在您应该能够按照您设置的方式重定向到您的控制器。
[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 });
}
In one or two occassions way back I ran into some issues by normal redirect and had to resort to doing it by passing a RouteValueDictionary. More information on RedirectToAction with parameter
在一两次情况下,我通过正常重定向遇到了一些问题,不得不通过传递RouteValueDictionary 来解决。有关带有参数的 RedirectToAction 的更多信息
return RedirectToAction("Index", new RouteValueDictionary(
new { controller = "ProductImageManager", action = "Index", id = id } )
);
If you get a very similar error but in lowercase 'id', this is usually because the route expects an id parameter that has not been provided (calling a route without the id /ProductImageManager/Index
). See this so questionfor more information.
如果你得到一个非常相似的错误,但是是小写的'id',这通常是因为路由需要一个尚未提供的 id 参数(调用没有 id 的路由/ProductImageManager/Index
)。有关更多信息,请参阅此问题。
回答by vohrahul
This should work!
这应该有效!
[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());
}
Notice that you don't have to pass the name of view if you are returning the same view as implemented by the action.
请注意,如果您返回与操作实现的视图相同的视图,则不必传递视图名称。
Your view should inherit the model as this:
您的视图应该继承模型,如下所示:
@model <Your class name>
You can then access your model in view as:
然后,您可以在视图中访问您的模型:
@Model.<property_name>