35

我想知道如何插入<input type="date"/>我模型的日期时间属性。至于现在 Razor 使用type="datetime". 我想利用 HTML5 的新输入类型。

4

5 回答 5

50

输入日期值格式需要按照https://www.rfc-editor.org/rfc/rfc3339#section-5.6 full-date 指定的日期。

所以我最终做了:

<input type="date" id="last-start-date" value="@string.Format("{0:yyyy-MM-dd}", Model.LastStartDate)" />

我确实尝试使用以下方法“正确”地进行操作:

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

@Html.TextBoxFor(model => model.LastStartDate, 
    new { type = "date" })

不幸的是,这似乎总是将输入的 value 属性设置为标准日期时间,所以我最终像上面一样直接应用了格式。

编辑:

根据 Jorn 的说法,如果你使用

@Html.EditorFor(model => model.LastStartDate)

而不是 TextBoxFor 一切正常。

于 2013-09-09T09:07:21.807 回答
34

我设法通过使用以下代码来做到这一点。

@Html.TextBoxFor(model => model.EndTime, new { type = "time" })
于 2013-01-21T15:17:06.877 回答
2
 @Html.TextBoxFor(m => m.EntryDate, new{ type = "date" })

 or type = "time"

 it will display a calendar
 it will not work if you give @Html.EditorFor()
于 2018-08-04T08:55:05.863 回答
0

如果您想使用 @Html.EditorFor() 您必须使用 jQuery ui 并使用 NuGet 包管理器将您的 Asp.net Mvc 更新到 5.2.6.0。

@Html.EditorFor(m => m.EntryDate, new { htmlAttributes = new { @class = "datepicker" } })

@section 脚本 {

@Scripts.Render("~/bundles/jqueryval")

<script>

    $(document).ready(function(){

      $('.datepicker').datepicker();

     });

</script>

}

于 2018-08-04T09:56:19.070 回答
-3

您将通过标签 type="date" 获得它......然后它将呈现美丽的日历和所有......

@Html.TextBoxFor(model => model.EndTime, new { type = "date" })
于 2016-04-14T09:29:47.420 回答