HtmlHelper extension method vs partial view?

旧街凉风 提交于 2019-12-04 22:35:59

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.

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.

Marko

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!