1

I would like to either override or create a custom function to wrap the RenderPartial shared function found in System.Web.Mvc.Html RenderPartialExtensions.

I found an article talking about this idea here: http://johncoder.com/Post/AdventuresinDebuggingAFriendlierCalltoRenderPartial

<% Html.TryRenderPartial("ClassB", Model.B); %>

In the example above, they have created a custom sub called TryRenderPartial that does some logic and calls RenderPartial if necessary. Unfortunately this article does not give a code example of the TryRenderPartial sub itself.

I can't figure out how to create this function because the RenderPartialExtensions is not inheritable. Also I'm not sure how to actually output the html because RenderPartial is a sub, not a function, so I don't get how to actually "return" the html. Any ideas?

6
  • What are you actually trying to do? This smells like and xy problem meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Sep 16, 2014 at 4:40
  • I have a view that has sections that need to be rendered based on the type of user. I'm trying to keep the view as clean as possible, so I thought I could create functions to wrap around the render partial, so for example RenderTypeA would only render the partial view if the user was Type A, RenderTypeB would only render the partial view if the user was Type B, etc. Basically the view has some additional sections if the user is a certain type, otherwise they are left out. Commented Sep 16, 2014 at 13:59
  • Well, you need only do something like this then: @Html.RenderPartial(GetUserPartial, GetUserModel) to retrieve the specific partial for that user and model for that user. No need to create a custom helper. You would create methods that you call to return the strings "PartialA" for UserA, and ModelA for UserA. Commented Sep 16, 2014 at 14:43
  • Actually, a better way would be to figure out these details in your controller, and include them in your model. So then you would just do this: @Html.RenderPartial(Model.UserPartialName, Model.UserModel) Commented Sep 16, 2014 at 14:45
  • Sorry, I should have mentioned that there are cases where partial views are rendered for one case but not another. For example Type A may have feature X that is in a partial view, but Type B does not have access to feature X. I'm basically trying to keep as much logic as I can out of the views and keep it as simple as possible. Commented Sep 16, 2014 at 15:20

1 Answer 1

1

It's literally trivial to wrap RenderPartial. You just create an HtmlHelper extension like so (in C#, I don't speak VB):

public static class MyRenderPartialExtensions
{
    public static void MyRenderPartial(this HtmlHelper htmlHelper, string partialViewName)
    {
        htmlHelper.RenderPartial(partialViewName)
    }
}

You would add similar methods for the other overloads you want to implement.

However, chances are, you probably don't really want to do this... most likely, what you want to do is already possible in a way that the framework exposes.

This is what's known as an XY Problem, which basically means that you have Problem X, and you have decided that you need to do Y to solve it. However, you can't figure out how to do solution Y, so you're asking how to do Y rather than asking how to do your original X problem.

The reason why XY problems are bad is because chances are, the solution you've decided you need to do is not the correct solution, and the reason you're having trouble with it is because it's not the right way to do things.

Frankly, I can't think of a good reason to wrap RenderPartial, since anything you'd do is more than likely doable in some other way.

To respond to your other comment, Html helpers don't "return" anything. That's why they're Sub's. How view rendering works is rather complex, and not a subject easily discussed in an SO answer.

HtmlHelpers don't work via inheritance, they use Extension Methods.

http://msdn.microsoft.com/en-us/library/bb384936.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.