Html MVC 4 Razor 添加输入类型日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14423888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4 Razor adding input type date
提问by Geethanga
I'm wondering how to insert a <input type="date"/>
for datetime attributes of my model.
As for now Razor creates a plain input element with type="datetime"
. I want to make use of the new input types of HTML5.
我想知道如何插入<input type="date"/>
我的模型的日期时间属性。至于现在 Razor 创建一个带有type="datetime"
. 我想利用 HTML5 的新输入类型。
采纳答案by Geethanga
I managed to do it by using the following code.
我设法通过使用以下代码来做到这一点。
@Html.TextBoxFor(model => model.EndTime, new { type = "time" })
回答by Giles Roberts
The input date value format needs the date specified as per http://tools.ietf.org/html/rfc3339#section-5.6full-date.
输入日期值格式需要按照http://tools.ietf.org/html/rfc3339#section-5.6full-date指定的日期。
So I've ended up doing:
所以我最终做了:
<input type="date" id="last-start-date" value="@string.Format("{0:yyyy-MM-dd}", Model.LastStartDate)" />
I did try doing it "properly" using:
我确实尝试使用以下方法“正确”执行此操作:
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime LastStartDate
{
get { return lastStartDate; }
set { lastStartDate = value; }
}
with
和
@Html.TextBoxFor(model => model.LastStartDate,
new { type = "date" })
Unfortunately that always seemed to set the value attribute of the input to a standard date time so I've ended up applying the formatting directly as above.
不幸的是,这似乎总是将输入的 value 属性设置为标准日期时间,所以我最终直接应用了上述格式。
Edit:
编辑:
According to Jorn if you use
根据 Jorn 的说法,如果您使用
@Html.EditorFor(model => model.LastStartDate)
instead of TextBoxFor it all works fine.
而不是 TextBoxFor 它一切正常。
回答by Md Shahriar
@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()
回答by Md Shahriar
If you want to use @Html.EditorFor() you have to use jQuery ui and update your Asp.net Mvc to 5.2.6.0 with NuGet Package Manager.
如果您想使用@Html.EditorFor(),您必须使用 jQuery ui 并使用 NuGet 包管理器将您的 Asp.net Mvc 更新到 5.2.6.0。
@Html.EditorFor(m => m.EntryDate, new { htmlAttributes = new { @class = "datepicker" } })
@section Scripts {
@section 脚本 {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function(){
$('.datepicker').datepicker();
});
</script>
}
}
回答by Madhusudhan V Indian
You will get it by tag type="date"...then it will render beautiful calendar and all...
您将通过标签 type="date" 获得它...然后它将呈现漂亮的日历和所有...
@Html.TextBoxFor(model => model.EndTime, new { type = "date" })