Hi everyone in this blog I’m explaining about HTML helpers and Partial views.
An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.
The following are some of the standard HTML Helpers already included in ASP.NET MVC:
Partial view is special view which renders a portion of view content. It is just like a user control web form application. Partial can be reusable in multiple views. It helps us to reduce code duplication. In other word a partial view enables us to render a view within the parent view.
The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view. If we made the change in this data (ViewDataDictionary object), the parent view's data is not affected. Generally, the Partial rendering method of the view is used when related data that we want to render in a partial view is part of our model.
HTML Helpers- are for reusable components. Web Grid, Pager, etc. These are distributed as assemblies and have no dependency on Razor. Choose this if:
- Functionality is truly reusable and applicable to any application
- You don't want people to modify it, want to version it
Partials Views: are a way to split large views into smaller parts to keep things more manageable. They are also useful for reusability that is specific to your application. These are located by the view engine, so you can have the same partial defined in different places (e.g. Views/Shared), allowing you to customize per controller, area or display mode. Choose this if:
- Functionality is application-specific
- Want to customize per controller, area or display mode
Local Helpers: are a way to execute the same template many times, without having to repeat yourself. You can also use it to break views into parts to avoid deep nesting, but keeping everything in the same file. Choose this if:
- Functionality is view-specific
Application Helpers: (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:
- Prefer Razor over TagBuilder
- Don't mind distributing files instead of assemblies
- Prefer type-safe method-call syntax instead of @Html.Partial(name)