Eslam Soliman
Eslam Soliman

Reputation: 1296

Redirect from action to another action in different controller in MVC

What I want to do is to redirect from an action in "A" controller to an action in "B" controller it gives me this error that it can not find this action in the target controller

I am using RedirectToAction Method any help

Upvotes: 2

Views: 28620

Answers (4)

sirbombay
sirbombay

Reputation: 419

I did this and it worked fine :)

  return Redirect("/B/index"); //assuming your controller is called BController. 

KISS - keep it simple sweetie ;) just pass the url to redirect as string

Upvotes: -2

Bao Duy
Bao Duy

Reputation: 79

You could try this:

return RedirectToAction("kiss", "meoghe");
// RedirectToAction("action name", "controller name"); 
// meogheController => "meoghe" is controller name

If you use ActionResult for return type on method, you must use return for RedirectToAction.

Upvotes: 6

Eslam Soliman
Eslam Soliman

Reputation: 1296

within your A controller add this in your action

RedirectToAction(new {controller="B", action="index", id=11,variable="abc"});

that's if you want to send some parameters also ,but do not forget to have a route configured which can map the "variable" parameter as well. I think this is solved in now

Upvotes: 3

Andras Zoltan
Andras Zoltan

Reputation: 42343

It's probably the case you've reversed the parameter values in your call to RedirectToAction - remember it's action first, then controller. Don't forget that controller is "Home", too, not "HomeController".

If that still doesn't work - then you've got the action name wrong; or the action you're redirecting to has a filter (e.g. [HttpPost]) that's preventing it from being able to be used.

Upvotes: 2

Related Questions