How to Call Another Controller Action from a Controller in MVC

How to call another controller Action From a controller in Mvc

Controllers are just classes - new one up and call the action method just like you would any other class member:

var result = new ControllerB().FileUploadMsgView("some string");

How to call a function written in one controller from another controller in mvc

You can solve this with inheritence.

Example:

public abstract class BaseController : Controller 
{
//Common functionality between controllers go here
protected static string Encrypt(string toEncrypt, bool useHashing)
{
//Your content
}
}

public class LoginController : BaseController //not Controller anymore
{
//Encrypt is now available

//...Your controller methods here
}

public class RegisterController : BaseController //not Controller anymore
{
//Encrypt is now available

//...Your controller methods here
}

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

Controller.RedirectToAction

Can we call the Method of a controller from another controller in asp.net MVC?

You could also simply redirect straight to the method like so:

public class ThisController 
{
public ActionResult Index()
{
return RedirectToAction("OtherMethod", "OtherController");
}
}

calling different Action Method from another Controller

If the method of StudentController is named GetStudent, then it needs to be

@Html.ActionLink("Get Student Info", "GetStudent", "Student")

The 2nd parameter is the name of the method your calling.



Related Topics



Leave a reply



Submit