Search code examples
asp.netrazorasp.net-mvc-4razor-2

How to call a view of another controller in mvc4


I have 2 controllers, SearchController and DetailsController.

the SearchController contains 2 views which contain form.

I want to redirect to a view of the details controller on the [HttpPost] action of my view in the SearchController

Is this possible???


Solution

  • You could try RedirectToAction if you are doing some processing in the first controller and then sending the result to another controller.

    View

    using(@Html.BeginForm("firstaction", "search", FormMethod.Post)){
    
       // form stuff
    }
    

    Controller

    public class SearchController
    {
    
          [HttpPost]
          public ActionResult FirstAction()
          {
              // do initial processing in the first controller
              // e.g persisting changed on Edit Screen
    
              return RedirectToAction("SecondAction","Details");
    
          }
    }
    
    public class DetailsController
    {
         public ActionResult SecondAction()
          {
              // do remaining processing in the first controller
              // fetching data for a grid and displaying the grid of items
    
              return View();
          }
    }