In this article I will explain with an example, how to use Html.Label and Html.LabelFor helper functions in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Model
The following Model class consists of one property Name to which the following validation Data Annotation attributes have been applied.
1. Display Data Annotation attribute.
public class PersonModel
{
    ///<summary>
    /// Gets or sets Name.
    ///</summary>
    [Display(Name = "Name: ")]
    public string Name { get; set; }
}
 
 
Controller
The Controller consists of the following Action methods.
Action method for handling GET operation
Inside this Action method, an object of PersonModel class is created and is returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        PersonModel person = new PersonModel
        {
            Name = "Mudassar Khan"
        };
 
        return View(person);
    }
}
 
 
View
Inside the View, in the very first line the PersonModel class is declared as Model for the View.
There are two Labels. The Label for the Name value is created using Html.LabelFor function while the Label for the Country value is created using Html.Label helper function.
@model Label_LabelFor_MVC.Models.PersonModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.Name)</td>
            <td><input type="text"/></td>
        </tr>
        <tr>
            <td>@Html.Label("Country: ")</td>
            <td><input type="text"/></td>
        </tr>
    </table>
</body>
</html>
 
 
Screenshot
ASP.Net MVC: Html.Label and Html.LabelFor example
 
 
Downloads