blog

Home / DeveloperSection / Blogs / HTML helpers and Partial Views

HTML helpers and Partial Views

Anonymous User3470 11-Mar-2015

Hi everyone in this blog I’m explaining about HTML helpers and Partial views.

HTML Helper:

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:

  1. Html.ActionLink()
  2. Html.BeginForm()
  3. Html.CheckBox()
  4. Html.DropDownList()
  5. Html.EndForm()
  6. Html.Hidden()
  7. Html.ListBox()
  8. Html.Password()
  9. Html.RadioButton()
  10. Html.TextArea()
  11. Html.TextBox()

Partial View:

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:

  1. Functionality is truly reusable and applicable to any application
  2. 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:

  1. Functionality is application-specific
  2. 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:

  1. Functionality is view-specific

Application Helpers: (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:

  1. Prefer Razor over TagBuilder
  2. Don't mind distributing files instead of assemblies
  3. Prefer type-safe method-call syntax instead of @Html.Partial(name)

Updated 22-Feb-2018
I am a content writter !

Leave Comment

Comments

Liked By