Advantages Of Razor Syntax And Difference Between @Html.Editorfor And @Html.TextBoxFor

First of all, we will look into some of the main advantages of use of Razor syntax.

  • It is light weight and it has a simple syntax.
  • It is easier to understand Razor syntax as compared to ASPX view engine syntax.
  • Provides automatic encoding of HTML output.
  • Razor syntax code is more succinct.
  • Provides great readability of markup and code.

Let us see the difference between @Html.Editorfor and @Html.TextBoxFor in Razor syntax ASP.NET MVC.

Code snippet for MVC model 

The MODEL in MVC app is given below. 

  1. public class ExternalLoginUser  
  2. {  
  3.     [Required]  
  4.     [Display(Name = "User name")]  
  5.     public string UserName {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [Display(Name = "Is User Active?")]  
  10.     public bool IsActive {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }   

Now, let us see the actual difference.

Html.EditorFor,

Razor syntax code snippet for Html.EditorFor

Explanation
  1. @Html.EditorFor(model => model. UserName)
    For the line given above, it renders a textbox for UserName model property.

  2. @Html.EditorFor(model => model. IsActive)
    For the line given above, it renders a checkbox control for IsActive model property, since model property is a kind of Boolean result.

Razor syntax code snippet for Html.TextBoxFor,

Html.TextBoxFor,

  • @Html.TextBoxFor(model => model. UserName)
  • @Html.TextBoxFor(model => model. IsActive)
Explanation

In this, it renders the textbox control for both UserName and IsActive model properties.

Ebook Download
View all
Learn
View all