Display a formatted date in a TextBoxFor()
C#asp.net MvcDateC# Problem Overview
I'm using MVC4 and Entity Framework to develop an intranet web app. I have a list of persons which I can edit. When I access the edit view, in the textbox "Start date", the date is displayed like this : 7/11/2013 00:00:00
. What I want to do is to display it in the format yyyy/MM/dd. I tried the String.Format("{0:yyyy/MM/dd}", item.StartDate)
but it does not work. I also tried with the annotation [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
but it does not work neither.
In my view I have this :
<div class="editor-field">
@Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker" })
@Html.ValidationMessageFor(model => model.StartDate)
</div>
Any idea about how to do?
C# Solutions
Solution 1 - C#
@Html.TextBoxFor(m => m.StartDate,
new { @Value = Model.StartDate.ToString("yyyy/MM/dd"), @class="datepicker" })
Solution 2 - C#
Your question asks for EditorFor()
but the code you provided uses TextboxFor()
.
In your Model (e.g. MyModel.cs), you should have:
public class MyModel
{
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
}
In your View (e.g. Index.cshtml), you simply use it like you wrote it:
@Html.EditorFor(m => m.StartDate, new { htmlAttributes = new { @class = "datepicker" } })
It works as expected.
By doing it that way (instead of altering the way it's displayed in your View), you could reuse your model somewhere else and don't have to specify how to display the date in your View. So, if for some reason, you have to change the display format, you would only need to change it once.
The solution also works for MVC 5.
Solution 3 - C#
The top easy for me , was adding the type attribute,
@Html.EditorFor(model => model.FechaRegistro, new { htmlAttributes = new { @class = "form-control oso" ,@type = "date" } })
Solution 4 - C#
Html.EditorFor also work
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "datepicker", @Value = model.StartDate.ToString("yyyy/MM/dd") } })
Solution 5 - C#
I'm a noob here (and also with MVC) and this is just an variation of Maxime's using a partial class...
If you've got a DB-first model and don't want to alter it directly for fear of losing your changes, just do this same thing in your own partial class. Just add a new class/file within your Models folder and make sure it uses the same namespace as your generated models.
So here's how you would declare the metadata to both format the date & modify the display name of another value (ie: to abbreviate the column headers in a table)
namespace MyProjectName.Models
{
public class MyModelMeta
{
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)] // format used by Html.EditorFor
public DateTime StartDate;
[Display(Name = "User ID")] // abbreviation shown in Html.DisplayNameFor
public string SomeReallyLongUserIDColumn;
}
[MetadataType(typeof(MyModelMeta))]
public partial class MyModel
{
}
}
Solution 6 - C#
try this.. convert the date value as a htmlAttribute within textboxfor.. that can solve your problem
<div class="editor-field">
@Html.TextBoxFor(model => model.StartDate, new { htmlAttributes = new { @Value = Model.StartDate.ToString("yyyy/MM/dd"), type = "date" }})
@Html.ValidationMessageFor(model => model.StartDate)
</div>
Solution 7 - C#
None of the above worked for me. Had to do the following: