David E
David E

Reputation: 47

Call from view an action from another controller

In ASP MVC 4, is it possible from a view to call another controller action?

I'm on http://localhost:57456/Archers and I would like to call a method from the controller Participe (So should be http://localhost:xxxxx/Participe/Action).

This is only applying to this action, so I don't want to redirect every action to this controller.

Upvotes: 0

Views: 1484

Answers (2)

Mustafa ASAN
Mustafa ASAN

Reputation: 3835

You can use Html.Action inside your view to call child actions

<div> @Html.Action("Action","Participate") </div>

Upvotes: 1

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

I would suggest you call your ParticipeController from the first action, and include the controller result into the view Data or the model to return to the view:

public class ArchersController {
    public ActionResult Index() {
        // your current code here

        // your custom call
        var result = new ParticipeController().Action("your params here");
        ViewData["ParticipeResult"] = result;

        // return View();
    }
}

Maybe though, some responsibility principles could be applied here in order to isolate the Participe call you want to make here into its own class or method.

Upvotes: 0

Related Questions