Mar*_*ark 12 c# asp.net asp.net-mvc google-chrome asp.net-mvc-3
我使用Google Chrome V28作为我的浏览器 - 我的模型上的DataAnnotations存在问题,这会迫使Chrome在我的视图中呈现数据时类型时使用它自己的内置日历.
我的模型是:
public class ObScore
{
public int ObScoreId { get; set; }
...
...
[DisplayFormat(DataFormatString = "{0:dd MMMM yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
...
...
}
Run Code Online (Sandbox Code Playgroud)
模型中肯定有数据:

...但在"修改"模式下显示时,Chrome会显示:

这是Chrome中的已知错误,我可以在代码中执行任何操作,将日期强制转换为表单吗?
谢谢,马克
Jif*_*ueb 15
很抱歉复活了一个6个月大的问题,但我遇到了同样的问题并得到了一个答案,我觉得这正是OP所期待的.
虽然mikhairu的回答确实有效,但它需要将类型更改为文本.OP的屏幕截图显示了Chrome的内置日期选择器UI,它要求输入为type = date.Chrome要求输入类型=日期的值采用YYYY-MM-DD格式,可能是为了避免任何特定于文化的歧义.
因此,在模型中,将此数据注释添加到日期属性中:
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Run Code Online (Sandbox Code Playgroud)
尝试删除[DataType(DataType.Date)]因为我相信这会创建<input type="date" />.如果你这样做,你最终会得到一个<input type="text" />可以附加jQuery日期选择器的东西.
尝试w3schools:在不同的浏览器中输入类型日期以查看差异.
编辑:
在过去,我使用以下内容View来使用jQuery日期选择器(如果您对使用它感兴趣).
@Html.TextBoxFor(model => model.DateOfBirth, @"{0:yyyy\/MM\/dd}", new { @class = "datepicker" })
要使用默认的 chrome datepicker 显示日期,您的模型需要两个属性:
[DataType(DataType.Date)]结果在<input type="date" />. 看到这个答案。[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]. yyyy-MM-ddchrome 日期选择器需要该格式。看到这个答案。例如:
模型
public class ObScore
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
剃刀视图
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@* Control with date picker *@
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11477 次 |
| 最近记录: |