Call action from another Controller - asp.net-mvc

I have a controller action.How can i call action method from another controller in asp.net mvc?
Is this a valid thing to do. Is it possible?

If you're first action is complete then you might redirect to another action.
If there is some common code between these two controllers that you don't want to duplicate then you'd probably look at creating a service which would have this and be called by both actions

Related

Creating a base controller default action for all ActionResults

I'm trying to find out if its possible to create a controller or action result in MVC that will trigger for every action.
Basically I want a check to occur before people are directed to the url each time, if this check fails I will return a different view but I don't want to write this out for every single action in my controller.
Can this be done??
This can be done with a custom ActionFilterAttribute.
Overriding OnActionExecuting means I can put the same check in for all actions.

StructureMap MVC 5 html.Action Issue

I am trying to call an Action from my view using #Html.Action("ActionName","controllerName"). But my page fails to load with below error:
A single instance of controller 'Web.Areas.Area1.Controllers.ActionController'
cannot be used to handle multiple requests. If a custom controller
factory is in use, make sure that it creates a new instance of the
controller for each request.
I am using structure map for Dependency injection. Please help me what am i missing.
You need to add
x.For<{Your controller name}>().AlwaysUnique();
in IoC.cs file. This should be done for every controller in your project.
For more details check this link.

Can we call two actionresult/controller at once in asp.net mvc 3?

Can we call Two ActionResult /Controller function at once which both return partial view?
No you can't call two actions at the same time using one URI. This is because the routing table will only match one route and then pass execution to that controller and action. You can call another action from within that action, but I wouldn't recommend it and would just have your client send another request to the server for that other action you want to run.

What's the Rails way to temporary branch to a different controller's action?

In my Rails controller I want to branch to a different controller's action, show its view and then return back to my original action, i.e. it's not a simple redirect, more like a sub-procedure call.
I want to use this whenever a user does something suspicious, like editing a post too often in a row. I still want to allow the edit, but first the user has to answer some CAPTCHA-like questions on a different page. (Similar to Authlogic-oid, where during validation the user is redirected to the OpenID provider's page)
You can push the current request into the Session, then do a redirect to the captcha, then have the captcha action look into the Session to check where it should redirect to.
IIRC, Autlogic does exactly that with redirect_back_or_default.
You can't.
Convert the common stuff into a helper method and then both the controllers should call this method.
It's possible to create an instance of the other controller and call methods on it, but you should probably reevaluate your organization first (look into helpers, modules, etc.)
#my_other_controller = MyOtherController.new
#my_other_controller.some_method(params[:id])

.NET MVC - Call a controller Action from another controller action

I have a controller action which I would like to call another controller action.
Is this a valid thing to do. Is it possible?
Controller.RedirectToAction
As #Justice says you can use RedirectToAction. Also, you can use TempData to pass model (and other) data between controller actions.

Resources