2

i have a model like this :

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd HH:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime DateUpdate { get; set; }

and have a view :

<div class="form-group">
        @Html.LabelFor(model => model.DateUpdate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Helpers.EditVal(Html.EditorFor(m => m.DateUpdate, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss") } }))
            @Html.ValidationMessageFor(model => model.DateUpdate, "", new { @class = "text-danger" })
        </div>
    </div>

but i can't save it because i get validation error for dateupdate field ! what is wrong here ? i want to save this format {0:yyyy/MM/dd HH:mm:ss} not mm/dd/yy or something else and i don't want to get validation error

SdSaati
  • 798
  • 9
  • 18

1 Answers1

4

I solved it with a quick change in my model :

[DataType(DataType.Date)] ======> [DataType(DataType.DateTime)]

and it works fine now

SdSaati
  • 798
  • 9
  • 18
  • after this i could write a custom validation too as this link say [link](http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/mvc-4-custom-validation-data-annotation-attribute/) – SdSaati Jul 11 '16 at 18:04