I need to call a controller B action FileUploadMsgView from Controller A and need to pass a parameter for it.
Code---its not going to the controller B's FileUploadMsgView().
In ControllerA
private void Test()
{
try
{//some codes here
ViewBag.FileUploadMsg = "File uploaded successfully.";
ViewBag.FileUploadFlag = "2";
RedirectToAction("B", "FileUploadMsgView", new { FileUploadMsg = "File uploaded successfully" });
}
In ControllerB receiving part
public ActionResult FileUploadMsgView(string FileUploadMsg)
{
return View();
}
This question is related to
c#
asp.net-mvc
asp.net-mvc-4
Let the resolver automatically do that.
Inside A controller:
public class AController : ApiController
{
private readonly BController _bController;
public AController(
BController bController)
{
_bController = bController;
}
public httpMethod{
var result = _bController.OtherMethodBController(parameters);
....
}
}
as @DLeh says Use rather
var controller = DependencyResolver.Current.GetService<ControllerB>();
But, giving the controller, a controlller context is important especially when you need to access the User
object, Server
object, or the HttpContext
inside the 'child' controller.
I have added a line of code:
controller.ControllerContext = new ControllerContext(Request.RequestContext, controller);
or else you could have used System.Web to acces the current context too, to access Server
or the early metioned objects
NB: i am targetting the framework version 4.6 (Mvc5)
if the problem is to call. you can call it using this method.
yourController obj= new yourController();
obj.yourAction();
As @mxmissile says in the comments to the accepted answer, you shouldn't new up the controller because it will be missing dependencies set up for IoC and won't have the HttpContext
.
Instead, you should get an instance of your controller like this:
var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
I know it's old, but you can:
Your sample looks like psuedo code. You need to return the result of RedirectToAction
:
return RedirectToAction("B",
"FileUploadMsgView",
new { FileUploadMsg = "File uploaded successfully" });
Dleh's answer is correct and explain how to get an instance of another controller without missing dependencies set up for IoC
However, we now need to call the method from this other controller.
Full answer would be :
var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
//Call your method
ActionInvoker.InvokeAction(controller.ControllerContext, "MethodNameFromControllerB_ToCall");
If anyone is looking at how to do this in .net core I accomplished it by adding the controller in startup
services.AddTransient<MyControllerIwantToInject>();
Then Injecting it into the other controller
public class controllerBeingInjectedInto : ControllerBase
{
private readonly MyControllerIwantToInject _myControllerIwantToInject
public controllerBeingInjectedInto(MyControllerIwantToInject myControllerIwantToInject)
{
_myControllerIwantToInject = myControllerIwantToInject;
}
Then just call it like so _myControllerIwantToInject.MyMethodINeed();
This is exactly what I was looking for after finding that RedirectToAction()
would not pass complex class objects.
As an example, I want to call the IndexComparison
method in the LifeCycleEffectsResults
controller and pass it a complex class object named model.
Here is the code that failed:
return RedirectToAction("IndexComparison", "LifeCycleEffectsResults", model);
Worth noting is that Strings, integers, etc were surviving the trip to this controller method, but generic list objects were suffering from what was reminiscent of C memory leaks.
As recommended above, here's the code I replaced it with:
var controller = DependencyResolver.Current.GetService<LifeCycleEffectsResultsController>();
var result = controller.IndexComparison(model);
return result;
All is working as intended now. Thank you for leading the way.
Source: Stackoverflow.com