通过参数mvc重定向到操作

[英]Redirect to Action by parameter mvc


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:

我的产品timagemanagercontroller:

[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

parameters dictionary包含不可空类型'系统的参数'ID'的空条目。Int32 System.Web.Mvc对方法。ViewResult指数(Int32)”“……Controllers.ProductImageManagerController”。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数

4 个解决方案

#1


15  

For redirect in the same controller you don't need to specify the controller. Not sure if you need to have the parameter nullable to do this redirect or if we have it as nullable because we need to in another sence, but this is from a working project:

对于同一控制器中的重定向,不需要指定控制器。不确定您是否需要参数nullable来执行重定向,或者我们将它设为nullable,因为我们需要在另一个ence中,但这是来自一个工作项目:

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

EDIT

编辑

Calling a different controller, you might need to use a RouteValueDictionary:

调用另一个控制器,您可能需要使用RouteValueDictionary:

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

The example you provided should work if your RouteConfig is configured for it, so you should check it so that you have it set up correctly. Check this stackoverflow question and answers for more information.

如果您的RouteConfig配置为它,那么您所提供的示例应该可以工作,因此您应该检查它,以便正确地设置它。检查这个stackoverflow的问题和答案以获得更多的信息。

EDIT 2:

编辑2:

By the comment from @Mohammadreza, the error was in the RouteConfig. To let the application handle URLs with an id, you need to make sure that the Route is configured for it. You do this in the RouteConfig.cs located in the App_Start folder.

根据@Mohammadreza的评论,错误出现在RouteConfig中。要让应用程序处理带有id的url,需要确保为其配置了路由。你在RouteConfig中这样做。位于App_Start文件夹中的cs。

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

#2


-1  

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>

#3


-2  

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.

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

#4


-3  

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

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

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/11/12/5362c0f644c4d8c012a74a4aaa054cba.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告