Return Action to an other controller - c#

I would like to return an Action to an other Controller
Example: i have 2 controlers:
[Route("myurl"]
public class HomeController : Controller
{
public ActionResult Action1()
{
if (...)
{
return Action2(); //working fine i will keep my route
}
else
{
return OtherController.Action3(); //Don't know how to do it here.
}
}
public ActionResult Action2()
{
return View();
}
}
and
public class OtherController : Controller
{
public ActionResult Action3()
{
return View();
}
}
It's working if the Action is inside the same controller but wish to return the Action1 from HomeController to Action3 from OtherController.
I want to keep the same route (not a redirection to an other route).
Any idea ?

Have you tried using this RedirectToAction?
return RedirectToAction("ACTION_NAME", "CONTROLLER_NAME", new { area = "" });

I found out:
I need to return like that :
return DependencyResolver.Current.GetService<OtherController>().Action3();
[Route("myurl"]
public class HomeController : Controller
{
public ActionResult Action1()
{
if (...)
{
return Action2(); //working fine i will keep my route
}
else
{
return DependencyResolver.Current.GetService<OtherController>().Action3();
}
}
public ActionResult Action2()
{
return View();
}
}

return RedirectToAction("ActionName", "ControllerName");

You can do this by calling the below method of controller class protected internal RedirectToRouteResult RedirectToAction(string actionName, string controllerName); For your requirement the code inside the else block would look like base.RedirectToAction("ACtion3","OtherController");

Related

How to return an existing view from a new action in ASP.NET MVC 4?

Noob needs help!) How can I return an existing view from a new action within the same controller?
For example I have a following code:
[HttpGet]
public ActionResult Index()
{
return View(); //returns Index.cshtml
}
[HttpPost]
public ActionResult Index(string id, string condition)
{
SomeModel.ID = id;
SomeModel.Condition = condition;
return View(SomeModel); //returns Index.cshtml delegating the model
}
public ActionResult someAction()
{
return View(); //How to make this action return Index.cshtml??
}
You can specify the view name to return:
public ActionResult someAction()
{
return View("Index");
}
public ActionResult someAction()
{
return View("Index"); //How to make this action return Index.cshtml??
}
Just add
return View("YourView");
If you want send a model to it you can do this
var model = new YourViewModel
{
}
return View("YourView", model);

Mvc3 C# - Is it possible to call an action from a different controller?

In my project I have two different controllers.
This is the main one:
public class Main : Controller
{
public ActionResult Index()
{
return View();
}
}
And this is the other one:
public class OtherOne : Controller
{
public ActionResult RandomAction()
{
return ... //more code here
}
}
What should I return in "OtherOne/RandomAction" in order to obtain the same result of "Main/Index" action?
It's as simple as this:
public class OtherOneController : Controller
{
public ActionResult RandomAction()
{
return RedirectToAction("Index", "Main");
}
}
Your Controller class names must be MainController and OtherOneController. If you want to change that, check this post:
change controller name convention in ASP.NET MVC
Here is your Main Controller:
public class MainController : Controller
{
public ActionResult Index()
{
return View();
}
}

How to: execute controller method (mvc4) in httpHandler

I have MVC4 applicaton, which contains http-handler, and I want to redirect user to my first page
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
HttpHandler
public class MyPublicHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (condition)
{
var controller = new HomeController();
controller.Index(); // redirect and execute
}
// or something like that
if (condition)
{
ExecutePage("http://myWebSite/Home/Index")
}
}
}
Thanks!

MVC Redirect to another Controller In different namespace, but same project

I have differnt controllers in my Website, Some of them are in WebSite/Controller folder and some are in Website/Area/Test/Controllers.
When I hit Website/Controller/Home/Index, I want to redirect the user to Website/Area/Test/Controller/Home/Index with Query string PArameter.
Here's is my first Controller
namespace mySite.Controllers
{
public partial class HomeController : BaseFrontController
{
public virtual ActionResult Index()
{
var issuburl = channelRepository.GetChannelByUrl('UserID');
if (issuburl != null)
return Redirect("~/Areas/Test/Controllers/Index");
return View();
}
}
}
and here's my Second Controller
namespace mySite.Areas.Test.Controllers
{
public partial class HomeController : BaseTestController
{
public virtual ActionResult Index(string param)
{
var chn = rep1.GetChannel(param);
if (chn != null)
{
model.Chn = chn;
}
else return Redirect("~/Error/Index");
return View();
}
}
}
my Error Controller is in Mysite/Controller folder and I can access it inside MySite/Area/Test/Controller, but How can I access Mysite/Controller controller inside
MySite/Area/Test/Controller
Below code is not working
return Redirect("~/Areas/Test/Controllers/Index");
Have you tried?
return RedirectToAction("Index", "controller", new { area = "Test" });
It is using RedirectToAction
To specify different parameters you can use
return RedirectToAction("Index", "controller", new { area = "Test", yourParam1 = "param1", yourParam2 = "param2" });
try this one:
return RedirectToAction("Index", "controller", new { area = "Test" });

Re-factoring Controller's Action in asp.net mvc3

I am writing this Action code (within same controller) more than 10 times for different Models. Is there any way i can reduce this code or how can i create a generic action.
[HttpPost]
public ActionResult SavePerson(Person p)
{
if (ModelState.IsValid)
{
//do something
return Redirect("/Main");
}
else
{
return View();
}
}
[HttpPost]
public ActionResult SaveCategory(Category c)
{
if (ModelState.IsValid)
{
//do something
return Redirect("/Main");
}
else
{
return View();
}
}
The main point is that //do something part always differs from action to action. So let's try to reduce all code other than that. You could use base controller for it
public class BaseController : Controller
{
[NonAction]
protected virtual ActionResult HandlePost<T>(T model, Action<T> processValidModel)
{
if (ModelState.IsValid)
{
processValidModel(model);
return RedirectToAction("Main");
}
else
{
return View(model);
}
}
}
And in derived controller
public class DerivedController : BaseController
{
[HttpPost]
public ActionResult Create(Person person)
{
return HandlePost(person, p => _repository.Save(p));
}
}
return ModelState.IsValid ? Redirect("/Main"):View();
as a start point would be the only line you need.
For functions which are going to be called too often, create a static class and define all such functions in that.
for example like following
public static class MyAppStaticClass
{
public static SavePerson(Person p)
{
... // your body
}
}
Then, you can refer it like MyAppStaticClass.SavePerson whenever you need it.

Categories

Resources