为什么我输入的日期不读的借鉴价值? [英] Why is my date input not reading the model value?

查看:135
本文介绍了为什么我输入的日期不读的借鉴价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个服务器端的家伙试图教自己一点的CSS,Javascript中,jQuery的等等。我写了一个加载了一个模型,并用简单的文本框中显示的值一个小测试项目。工程确定,因为你可以看到:

I'm a server side guy trying to teach myself a bit of CSS, Javascript, jQuery etc. I have written a little test project that loads up a model and displays the values in simple text boxes. Works OK, as you can see:

但当然,我想适当地显示这些日期。因此,让我改变那些输入类型DATE。这里的剃刀code:

But of course, I want to display those dates appropriately. So let me change those input types to "date". Here's the Razor code:

@Html.TextBoxFor(m => m.Date, new { @type="date", @id="ondate" })

好了,工作....排序。我的意思是,它现在显示为一个日期选择器...但它不再显示该模型的日期!

Well, that worked.... sort of. I mean, it now displays as a date picker... but it's no longer displaying the model's date!

我在做什么错了?

推荐答案

键入=日期属性使浏览器的HTML5日期选择器。为了这个正常工作,格式必须是 YYYY-MM-DD (ISO格式),所以它需要

The type="date" attribute renders the browsers HTML5 datepicker. In order for this to work correctly, the format needs to be yyyy-MM-dd (ISO format), so it needs to be

@Html.TextBoxFor(m => m.Date, "{0:yyyy-MM-dd}", new { @type="date" })

的财产或者,您可以设置数据属性

Alternatively you can set data attributes on the property

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

和使用

@Html.EditorFor(m => m.Date)

它增加了键入=日期属性,并使用正确的格式。

which adds the type="date" attribute and uses the correct format.

旁注:


  1. HTML5的日期选择器只能在最新版本的Chrome浏览器的支持

  2. 使用 EditorFor()(在MVC-4)将不会允许您设置 ID
    属性,但其并不清楚你为什么会需要改变
    默认的 ID =日期 ID =ondate

  1. The HTML5 datepicker is only supported in recent versions of Chrome
  2. Using EditorFor() (in MVC-4) will not allow you to set the id attribute, but its not clear why you would need to change the default id="Date" to id="ondate"

这篇关于为什么我输入的日期不读的借鉴价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆