How to format the Textbox with Date DisplayFormat in ASP.NET MVC Razor View ?

There are times when you want to format the textbox with date display format when developing a ASP.NET MVC Razor View application.

You can format the Textbox with Date DisplayFormat in ASP.NET MVC Razor View easily by setting the DisplayFormat attribute for the property in the Model and then bind ito the textbox.

Below is a sample sourecode demonstrating the formatting of Textbox with Date DisplayFormat in ASP.NET MVC Razor View .

Model

public class EmployeeModel

{

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:DD/MM/YYYY}")]

public DateTime DateOfBirth{ get; set; }

public string Name { get; set; }

}

View (Razor)

@Html.EditorFor(property => property.DateOfBirth)

Leave a Comment