Validating time-only input in asp.net MVC unobtrusive validation

后端 未结 3 1367
野性不改
野性不改 2020-12-11 01:08

I have two separate fields on the page: one for date and one for time.

This is the model:

[Required]
[DisplayFormat(ApplyFormatInEditMode = true, Dat         


        
3条回答
  •  我在风中等你
    2020-12-11 01:45

    Add DataType.Time attribute to your time field and use EditorFors to remove format duplication:

    Model

        [Required]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
        [DataType(DataType.Time)]
        public DateTime? StartTime { get; set; }
    
        [Required]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime Date { get; set; }
    

    View

        @Html.EditorFor(m => m.Date, new { type = "text" })
        @Html.EditorFor(m => m.StartTime, new { type = "text", id = "timeStart" })
    

提交回复
热议问题