views:

128

answers:

2

If I have say a Partial View called MypartialView and I have a HTML Helper called "MyHTMLHelper" how can I return a partial view from the helper?

My requirement is that sometimes I'd like to render a PartialView on it's own and other times I'd like to render it with another partial view, or a slab of text or something.

So I thought I could create a helper that return both partial views, and a html helper that would return the partial view along with a slab of text.

is this best practice or should i instead create a partial view that has both partials in it and another that has a the partial view + the slab of text?

I'm not only looking for source but also the best practice according to what people are doing.

thanks.

A: 

I would use two Views:

-With 2 to partials

<% Html.RenderPartial("Partial1"); %>
<% Html.RenderPartial("Partial2"); %>

-The Partial and some text

Some Text
<% Html.RenderPartial("Partial1"); %>

I think the concept of DRY is still there, because at the end you still have all the code in one place, the Partial Views, and you just reference it from another two Views.

Doing it the other way will be complicated, and I don't think its really necessary to use another Helper Method to accomplish this.

Omar
A: 

Helpers seem to be designed to be reused a lot more heavily than partials so i'd suggest that if you think you will use the rendered result from the helper as much as you would with the alternative method (nested PV) then go with the helper.

cottsak