Problem with MVC ModelMetaData attributes and date formatting

I am trying to figure out how to format a date using MVC3 RC2 and a model decorated with DataAnnotations.

This is my model ...

public class Model
{
    [Display(Name = "Date of Birth")]
    [DisplayFormat(@DataFormatString = "MM/dd/yyyy")]
    public DateTime DateOfBirth { get; set; }
}

This is my controller ...

public class IndexController : Controller
{
    public ActionResult Index()
    {
        return View( new Model() {DateOfBirth = DateTime.Now});
    }
}

It's my opinion...

@model MvcApplication6.Models.Model

@Html.LabelFor(m=>m.DateOfBirth)
@Html.TextBoxFor(m => m.DateOfBirth)
@Html.EditorFor(m => m.DateOfBirth)

When I launch the site, for both controls the page displays a text box with the time included in the line, when all I want is the date (as indicated in the decorated property in the model).

I used the Brad Wilson article for ModelMetaData for this template.

Is it obvious what I'm doing wrong?

+3
source share
2 answers

Do it:

[DisplayName("Date of Birth")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateOfBirth { get; set; }
+7
source

, , DefaultModelBinder datetimes ( , front-end DateTime, [ CastleMonorail ] MVC ).

, . - , , , .

, , , DateTime. : , , datetime. .

0

Source: https://habr.com/ru/post/1784151/


All Articles