William Niu
William Niu

Reputation: 15853

How to refer to a partial view of another controller in a view?

The title pretty much states my question.

I have a view (say, Action1) in a controller (Foo) and another view (Action2) in another controller (Bar). In Action1 view, I want to use Html.Partial or Html.RenderPartial to call Action2's view.

I am aware of that I can use Html.RenderAction in Action1, but that will (I think) create the Bar controller and go through the whole controller/action resolution cycle, and I don't want that, as this may be less efficient.

So, my goal is that I want to reuse the Action2's veiw. How can I achieve this?

Upvotes: 0

Views: 529

Answers (1)

RPM1984
RPM1984

Reputation: 73123

Consider moving the partial to the Views\Shared folder.

Then you can render it from anywhere:

E.g:

Razor:

@Html.Partial("SharedView")

ASPX:

<%: Html.Partial("SharedView") %>

If you don't want to do that, then pull as much markup from the main partial as you can into a custom display template, then re-use that across the two Views.

And yes, your right about @Html.Action going through the request pipeline, and @Html.Partial not.

Upvotes: 1

Related Questions