Always keep your view DRY (do not repeat). Suppose your website has any of the following,
- Top menu
- Left menu
- Right menu
Or you have some HTML which is repeated in different view, then you could use the following,
@Html.Partial(“ViewFile”)
Renders the “ViewFile” view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).
@Html.RenderPartial(“ViewFile”)
Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, @Html.RenderPartial(“MyView”) won’t work. You have to wrap the call in a code block instead: @{Html.RenderPartial(“ViewFile”);}.
@RenderPage(“ViewFile.cshtml”)
Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). However, it seems to always use the current view’s model as the model for “ViewFile.cshtml”.
I prefer using @RenderPage(“ViewFile.cshtml”) only because the syntax is easier and it is more readable. If you are using MVC Area RenderPage by default will look into the View and Shared folder of the Area. If you want to point it to the common View folder in the root, then you have to mention the path. See example below.
@RenderPage(“~/Views/Shared/ViewFile.cshtml”)
References,
Leave a Reply