1

I have the following model for a person:

public class Person
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Id")]
    public int Id { get; set; }

    [Display(Name = "Name")]
    [StringLength(30)]
    public string Name { get; set; }

    [Display(Name = "Last Name")]
    [StringLength(30)]
    public string LastName { get; set; }

    [Display(Name = "Birth date")]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }

    [Display(Name = "Height")]
    [Range(120,250, ErrorMessage="Wrong height!")]
    public int Height { get; set; }
}

When I create a new Person, it creates normally in database with the following date format for BirthDate: dd.mm.yyyy

The problem is when I try to edit details of that person, birth date is not displayed inside EditorFor field. All other details like name, last name and height are displayed but birth date not.

So, this all displays all saved values correctly:

@Html.EditorFor(model => model.Person.Name)
@Html.EditorFor(model => model.Person.LastName)
@Html.EditorFor(model => model.Person.Height)

But this doesn't display anything or in some browsers it displays just date format without values:

@Html.EditorFor(model => model.Person.BirthDate)

When I look at the source code of the web site, I can see there is a value which represents a date. It's just not displayed. I also tried to populate it with javascript but without success. Any ideas?

Thank you!

Cristiano
  • 3,099
  • 10
  • 45
  • 67
  • you probably need to do a @Html.EditorFor(model => model.Person.BirthDate.ToString()) – Prabhu Murthy May 05 '13 at 09:36
  • You can try using a format string for the datetime, incase you have a different datetime configured in your m/c than the one specified by your culture. Sample: `@Html.EditorFor(model => model.BirthDate.ToString("dd/MM/yyyy"))` – Saravanan May 05 '13 at 09:39
  • Also refer to the Question here: http://stackoverflow.com/questions/7124434/display-only-date-and-no-time-c-sharp – Saravanan May 05 '13 at 09:42
  • @saravanan - Thanks but when I put `ToString("dd.MM.yyyy")` I'm receiving the following error: `Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.` – Cristiano May 05 '13 at 12:01
  • In that case and considering the below solution by @webdeveloper, you can render a text box and set a jQuery `datepicker` or else use the attribute on the property, since this will not display date in chrome for cross browser compatibility, we are using only the jQuery datepicker where in you can initialize the calendar based on a format string too. – Saravanan May 05 '13 at 12:07
  • Now it works! The problem is that know it throws an exception if user sets a year lower than 1753. I googled a little bit and I think I will need to set some custom validation inside my model. What do you think? Can I set allowed year range somehow differently= – Cristiano May 05 '13 at 13:51
  • Possible duplicate of [MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in Internet Explorer](http://stackoverflow.com/questions/12633471/mvc4-datatype-date-editorfor-wont-display-date-value-in-chrome-fine-in-interne) – podiluska Feb 22 '17 at 13:39

1 Answers1

7

Look at this question: MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in IE

One of possible solutions:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • Now the only problem is that it sets a year to 0001 and if the year is lower than 1753, it throws an exception. I can set a year in controller to a more acceptable value (like 2013) but I was wondering do you maybe know how can I set a year range for DateTime so it will not pass validation if user sets lower year than 1753? – Cristiano May 05 '13 at 13:48
  • 1
    @ZdravkoVajudin Create your own attribute, in this article: http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx Look at `DateRangeAttribute` code, you should remove `MaxDate` and use `Datetime.UtcNow` (or `Now`) – webdeveloper May 05 '13 at 19:35