How to use Html.Partial and Html.RenderPartial
Html.Partial and Html.RenderPartial are both methods used to render partial views in a view. Their usage is as follows:
- Html.Partial method:
- Display the PartialView named “PartialViewName” using HTML.
- This will render a partial view called “PartialViewName.cshtml”.
- Include the partial view named “PartialViewName” with the given model.
- This will render a partial view with model data.
- The Html.RenderPartial method:
- Display the partial view named “PartialViewName” using HTML.
- This will directly output the HTML of the partial view to the current view.
- Display the partial view named “PartialViewName” with the corresponding model.
- This will directly output the HTML of the partial view with model data into the current view.
It is important to note that the Html.RenderPartial method outputs HTML directly to the view, while the Html.Partial method returns the HTML of a partial view as a string to the view. Additionally, the performance of Html.RenderPartial method is slightly better than Html.Partial method because it directly outputs HTML without needing additional string operations.