Redirect to Action by parameter mvc

2019-04-19 19:13发布

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

4条回答
甜甜的少女心
2楼-- · 2019-04-19 19:48

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 }
    );
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-19 19:57

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楼-- · 2019-04-19 20:04

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>
查看更多
爷、活的狠高调
5楼-- · 2019-04-19 20:07
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

查看更多
登录 后发表回答