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.
- public class ExternalLoginUser
- {
- [Required]
- [Display(Name = "User name")]
- public string UserName {
- get;
- set;
- }
- [Display(Name = "Is User Active?")]
- public bool IsActive {
- get;
- set;
- }
- }
Now, let us see the actual difference.
Html.EditorFor,
Razor syntax code snippet for Html.EditorFor
Explanation
- @Html.EditorFor(model => model. UserName)
For the line given above, it renders a textbox for UserName model property.
- @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.