重定向到行动由参数MVC [英] Redirect to Action by parameter mvc

查看:143
本文介绍了重定向到行动由参数MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重定向到其他控制器的动作,但它不工作
这里是我的code在ProductManagerController:

  [HttpPost]
公众的ActionResult RedirectToImages(INT ID)
{
    返回RedirectToAction(指数,ProductImageManeger,新{ID = ID});
}

和这在我的ProductImageManagerController:

  [HTTPGET]
公众的ViewResult指数(INT ID)
{
    返回视图(指数,_ db.ProductImages.Where(RS => rs.ProductId == ID).ToList());
}

重定向到不带参数ProductImageManager /指数非常好(没有错误)但高于code I得到这样的:


  

参数词典共包含空值条目
  参数非可空类型'System.Int32'的'ID'的方法
  System.Web.Mvc.ViewResult指数(Int32)已在
  ShoppingCart.Areas.Admin.Controllers.ProductImageManagerController。
  一个可选参数必须是引用类型,可空类型,或者是
  声明为可选参数。参数名:参数



解决方案

有关重定向在同一个控制器不需要指定控制器。不知道你是否需要有参数为空的做这个重定向或者,如果我们把它作为空,因为我们需要在另一个SENCE,但是这是从工作项目:

  [HTTPGET]
公众的ActionResult EditRole(INT?selectedRoleId)
{
    AddEditRoleViewModel角色= _userService.GetAllRoles(selectedRoleId);
    返回查看(作用);
}[HttpPost]
公众的ActionResult EditRoleSave(AddEditRoleViewModel角色)
{
    _userService.SaveRole(作用);
    返回RedirectToAction(EditRole,新的{selectedRoleId = role.Id});
}

修改

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

 返回RedirectToAction(指数,新RouteValueDictionary(
    新{控制器=ProductImageManager,行动=索引,ID = ID})
);

如果您的 RouteConfig 配置它,所以你应该检查一下,让你有它正确设置您所提供的例子应该工作。检查这个计算器问题和答案以获取更多信息。

编辑2

通过从@Mohammadreza的评论,错误是在 RouteConfig
为了让应用程序处理的URL与ID,您需要确保该路由配置它。您在 RouteConfig.cs 要这样做位于 App_Start 的文件夹中。

 公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);    //添加{ID},因此你不需要使用它的每一个动作设定为可选
    routes.MapRoute(
        名称:默认,
        网址:{控制器} / {行动} / {ID}
        默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
    );
}

I want to redirect to an action in other Controller but it doesn't work here's my code in ProductManagerController:

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

and this in my 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:

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 'ShoppingCart.Areas.Admin.Controllers.ProductImageManagerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

解决方案

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:

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

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.

EDIT 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.

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

这篇关于重定向到行动由参数MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆