user1139666
user1139666

Reputation: 1697

ASP .NET MVC 3 + Calling an HttpPost Action Method From a Different Controller

I am currently developping a full-web application under VS2010 and I am using the ASP .NET MVC 3 framework.

Here is a simplified overview of my application :

I want ActMeth1 to call ActMeth2 to perform some logic and then to return View2.

Here is the source code of ActMeth1 :

public ActionResult ActMeth1()
{
    Ctrl2 myCtrl2 = new Ctrl2();
    return myCtrl2.ActMeth2();
}

Unfortunately ActMeth1 returns View1.
Does someone can give me an explanation to this fact ?
Thanks in advance for your future help

Upvotes: 0

Views: 1588

Answers (2)

chandmk
chandmk

Reputation: 3481

Instantiating a controller's action method in another controller's action method is inviting trouble in the long run.

You can use tempdata, or pass the data via route dictionary of RedirectToAction.

I think you better reorganize your logic

As you are trying to do this logic in server side anyway,

a. Create a service that does the work for both the controllers b. make the view shared between both the controller actions or create a partial view for the common html c. Call the appropriate service method and render the shared view

Upvotes: 1

Ian Routledge
Ian Routledge

Reputation: 4042

You could do:

public ActionResult ActMeth1()
{
    Ctrl2 myCtrl2 = new Ctrl2();
    myCtrl2.ActMeth2();
    return View("~/Views/Ctrl2Views/View2.cshtml");
}

I'm not sure you should be instantiating controller 2 from inside controller 1 though...

Upvotes: 0

Related Questions