How to call or return partial view of another controller from one controller?


When working in ASP.NET MVC, I have a partial view located inside "Views -> FirstController->_PartialView1.cshtml", I would like to call this partial view of "FirstController" from "SecondController", how can I call it in ASP.NET MVC?

The reason I am doing so is the HTML used is same, but with different data.


Asked by:- manish
0
: 9274 At:- 7/31/2018 3:24:38 PM
MVC C# partial view







3 Answers
profileImage Answered by:- Vinnu

This is not a recommended way, but you can call Another Controller partial view like below

   public PartialViewResult SomeActionName()
        {
           //some code here
            return PartialView("~/Views/ControllerName/_PartialView.cshtml", YourModel);
        }

or if PartialView in another Area

   public PartialViewResult SomeActionName()
        {
           //some code here
            return PartialView("~/Areas/AreaName/Views/ControllerName/_PartialView.cshtml", YourModel);
        }

Best Practice is call PartialView from inside the view using Render partial or Partial

Html.RenderPartial("~/Views/ControllerName/ViewName.cshtml", modeldata);

Or using 

@Html.Partial("~/Views/ControllerName/ViewName.cshtml", modeldata)

That's it.

Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder.

So, basically you need to Create a Folder inside Views->Shared->SharePartialView ->_CommonPartialView.cshtml

And then call it from controller as mentioned above.

   public PartialViewResult SomeActionName()
        {
           //some code here
           return PartialView("~/Views/Shared/SharePartalView/_CommonPartialView.cshtml");
        }

That's it.

2
At:- 8/2/2018 6:50:09 AM


profileImage Answered by:- SnehalSawant

This link may help you.

https://stackoverflow.com/questions/21089917/how-to-return-partial-view-of-another-controller-by-controller

0
At:- 8/1/2018 10:18:20 AM


profileImage Answered by:- neena

You can call HTML.Action also from the view.

@Html.Action("Action", "Controller", new {id = model.id})

from the main view.

OR

You can directly also use RenderPartial

@Html.RenderPartial("~/Views/MainController/View.cshtml",Model)

Thanks

0
At:- 6/10/2021 11:00:47 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use