HTML Helper Methods of MVC


Introduction

This article describes little about the default used HTML Helper Methods in the MVC Layout page.

Getting Started

  1. Url.Content():This method specifies the path of any file that we are using in our View code. It takes the virtual path as input and returns absolute path.
  2. Html.ActionLink()

    Used to render HTML links that links to action of some controller. Specially this method is used in the layout page to display menus.

     @Html.ActionLink("Home", "Index", "Home")  
    
    The first parameter specifies the display name, the second parameter specifies the Action name and the third parameter specifies the Controller name.

  3. RenderSection():

    Specifies the name of section that we want to display at that location in the template, it when you want to render optional content sections in a layout page. For example, if a user of your website lives in a certain region, the code on a content page could add a section that displays special pricing packages for that region. To specify that a section is optional and should only be rendered if it exists, pass false to the required parameter.

  4. RenderBody(): Renders the actual body of the associated View, means it just replace RenderBody from layout page by inserts body of child view when you writes below code in a child view.
     @{  
       Layout = "~/Views/Shared/_Layout.cshtml";  
     }  
    
  5. Scripts.Render()

    This method is used to render the javascript and css into page before calling RenderBody() method.

     @Styles.Render("~/Content/css")  
     @Scripts.Render("~/bundles/modernizr")  
    

Summary

In this article we have discussed the important HTML helper method used int layout page in MVC, hope this article will be helpful for you.

Thanks