TextBoxFor show date without time

蹲街弑〆低调 提交于 2019-12-11 01:39:23

问题


I want to show date in text field in format MM/dd/yy. I described model class as:

public class VacancyFormViewModel
{
    public int? ID { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy}")]
    public DateTime SurgeryDate { get; set; }

Of course, I can use EditorFor instead of TextBoxFor:

@Html.EditorFor(p => p.SurgeryDate, new { @class = "form-control" })

Problem is css styles, customer wants apply bootstrap styles, which are broken:

if I set TextBoxFor instead of EditorFor I get the date with time:

@Html.TextBoxFor(p => p.SurgeryDate, new { @class = "form-control" })

Any way to solve my problem? Or apply styles for EditorFor, or show TextBoxFor without time, or third way?


回答1:


You can specify date format in TextBoxFor helper like this:

@Html.TextBoxFor(m => m.SurgeryDate, "{0:MM/dd/yy}",  new { @class = "form-control" })


来源:https://stackoverflow.com/questions/28355155/textboxfor-show-date-without-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!