0

I am thinking about a scenario rather similar to this question asked some time ago: How to call another controller Action From a controller in Mvc

In the scenario I am thinking of, controller B would however return a partial view that I would like to incorporate into the ViewResult returned by the Home controller, so the code might look like this:

public class Home
{
    public ActionResult Index()
    {
        ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction", 
                                                              controllerName: "Controller_B");
        
        // The returned view should include the partial view `partialViewFromCtrl_B`
        return View();
    }
}

public class Controller_B
{
    public ActionResult SomeAction()
    {
        // Do some stuff

        return PartialView(viewName: "_SomeAction", model: someModel);
    }
}

Is there a way to combine the partial view returned from controller B into the view returned from the Home controller? If possible, I would like to keep the logic and the views defined for controller B separate from the Home controller rather than merging everything into the Home controller.

Your help is very much appreciated!


Edit: One possible solution to my scenario might by if the Index.cshtml of the Home controller looks like this

@model SomeModelType
...
@Html.Partial(partialview: "~/Controller_B/_SomeAction", model: Model)
...

and the Index() action of the Home controller would look like this:

public ActionResult Index()
    {
        // Get the model from Controller_B instead of the partial view
        SomeModelType someModelFromCtrl_B = new Controller_B().returnSomeModel();
        
        return View(someModelFromCtrl_B);
    }

This however does not seem to be the best approach to take here for me.

So if possible I would like to combine the partial view Controller_B/_SomeAction and the Index view of the Home controller in the action method of the Home controller if that is possible.

CJS
  • 68
  • 7
  • 3
    Generally speaking, when you find yourself trying to do stuff like this, it's time to rethink your approach. Take a step back and reconsider your design. Please provide more context as to the reason behind the sharing so we can help you. If you are going to share the other controller's method, you might as well just call that controller, unless what you want is to share a chunk of rendering in which case you should be doing the calling from the view itself. – JuanR Feb 15 '22 at 16:11
  • 1
    The Index view itself could request the partial view - in fact, that's how it's supposed to work. Is there a problem rendering the partial view in the Home/Index view? – haldo Feb 15 '22 at 16:11
  • 2
    Have you considered a Child Action https://stackoverflow.com/q/12530016/2030565 – Jasen Feb 15 '22 at 18:28
  • Thank you so much! I totally wasn't aware of that, but it looks exactly like the approach I was looking for. – CJS Feb 15 '22 at 18:42
  • I'd be happy to accept that as an answer if you post it as such. – CJS Feb 15 '22 at 18:50

1 Answers1

1

Through the code posted I can not understand what you want to return. based on your little code I think you intend to return Index values in PartialView. Take a look at the code below:

public class Home
{
  public ActionResult Index()
  {
    if(//cond){
        // Do some stuff
        return PartialView(viewName: "_SomeAction", model: someModel);          
    }       
    else{
        ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction", 
                                                          controllerName: "Controller_B");        
        // The returned view should include the partial view `partialViewFromCtrl_B`
        return View("Index");           
    }       
  }
}
Miguel
  • 139
  • 8
  • 1
    Thanks a lot for your reply. Your answer is however not exactly what I intended to do. Let me edit my question and try to be a more precise. – CJS Feb 15 '22 at 18:08