Time Input Field in ASP.NET MVC

前端 未结 4 1738
萌比男神i
萌比男神i 2020-12-21 08:29

I\'m having trouble trying to write a form with time field using ASP.NET MVC 4.

Model:

class Abcde {
    [DataType(DataType.Time)]
    public DateTim         


        
相关标签:
4条回答
  • 2020-12-21 09:10

    Use:

    [Required]
    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
    public DateTime FieldTime{ get; set; }
    
    0 讨论(0)
  • 2020-12-21 09:12
    [Required]
    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:H:mm}")]
    public DateTime Time { get; set; }`
    

    Try this, you can format in the DataFormatString the way you want it to work for you, im using it in a project now and it's working for me.

    0 讨论(0)
  • 2020-12-21 09:17
    [Required]
    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
    public DateTime Time { get; set; }
    

    This code is correct. But it only works when you use EditorFor instead of TextBoxFor.

    0 讨论(0)
  • 2020-12-21 09:19

    Actually this is what worked for me.

    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
    public DateTime Hour { get; set; }
    
    0 讨论(0)
提交回复
热议问题