MVC Datatype ErrorMessage

前端 未结 2 1804
栀梦
栀梦 2021-01-12 13:11

DataType ErrorMessage doesn\'t work. MVC4 DataType ErrorMessage doesn\'t seem to work. I have this dataannotation Attribute:

[DataType(DataType.DateTime, Err         


        
相关标签:
2条回答
  • 2021-01-12 13:52

    Short Answer: Purpose of DataType.DateTime is NOT to validate your DateTime entry for Birthday property. This is the reason. What it does is just formats the DateTime before displaying it on your view.

    What you need is to have [Required] attribute on top of that as well.

    However, what i usually prefer to use is Jquery Datepicker and it doesn't even allow user to enter any text, but a valid date.

    Edit: When you decorate a model property with [DataType(DataType.Date)] the default template in ASP.NET MVC 4 generates an input field of type="date". Browsers that support HTML5 such Google Chrome render this input field with a date picker.

    You may enforce this with code:

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? BirthDate { get; set; }
    

    In order to correctly display the date, the value must be formatted as 2012-09-28

    0 讨论(0)
  • 2021-01-12 13:57

    On MVC 4 I was able to change the error message in the View with Html.ValidationMessageFor.
    See an example:@Html.ValidationMessageFor(model => model.FechaDesde, "Fecha Inválida")

    0 讨论(0)
提交回复
热议问题