Question

I curious when it's recommended to use HtmlHelper extension method and when to use partial view? They seem to be equally eligible in many cases.

Was it helpful?

Solution

I personally think that partial view is more or less similar to usercontrol in asp.net, which act as a group of reusable functional elements. For example, if you need to make a login control box that may appear across the whole site, partial view would be better. What's more for partial view is, it is actually self-contained with its own rendering/processing strategy (and may have its own state as well)

On the other hand, htmlhelper is just tools for saving coding time and maintain, somehow better readability on your codes.

OTHER TIPS

HtmlHelpers - small bits of HTML (custom links, buttons, inputs, etc)

PartialViews - more complex things (forms, headers, footers, list items, etc)

It's much easier to write tests for HTML helpers (which are just methods) than partial views. If I'm writing something that has logic, such as different output based on the contents of viewdata, then I use a helper with tests around it. If I'm creating a piece of reusable content without any real logic, then I'll probably use a partial view.

Just to add: you will use htmlhelper for rendering elements in usercontrol, if existing htmlhelpers can't do the job...so let put it as a rule:

In scenario when you using htmlhelper instead of usercontrol(partial view) is bad approach and scenario when you using htmlhelper as a helper for rendering atom elements of a page/usercontrol is good approach

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top