-1

I am calling KitchenController from HomeController, how can I get KitchenController's Model in HomeController.

        public void KichenDetails(string KichenId)
        {
            if (string.IsNullOrEmpty(KichenId))
                KichenId = "";
            var result = new KitchenController().Index(KichenId);
            result.Model.;// result contains model and other details as well 
        }
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Sunil Chaudhary
  • 397
  • 1
  • 9
  • 31
  • I think you will get help from here [link](https://stackoverflow.com/questions/16870413/how-to-call-another-controller-action-from-a-controller-in-mvc) – ishan joshi Jun 13 '17 at 05:21
  • **[Something like this](https://stackoverflow.com/questions/15385442/passing-data-between-different-controller-action-methods)** – Guruprasad J Rao Jun 13 '17 at 05:22

1 Answers1

2
  1. This is a sign of bad system design and you should never create controller by yourself. Controllers are supposed to be created by controller factory as part of request handling flow. In addition to that in most of the applications controllers, interact with some services/external dependencies which should be resolved by using dependency injection.

  2. Controller Actions mostly return ActionResults (or some class that derive form it - ViewResult, JsonResult...) or directly flush data into response. So even if could call

    var result = new KitchenController().Index(KichenId);
    

    You won't be getting the result that you expect. Model, is a private variable inside the controller action that is used to render a view or it is serialized into JSON.

If you have a shared logic that is used to create a model for Index action and KichenDetails method you should extract this logic into a separate service/provider/factory class this will be used in both methods. This class could

Alex Art.
  • 8,711
  • 3
  • 29
  • 47