Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc asp.net mvc HTML5日期输入类型_Asp.net Mvc_Html_Datepicker - Fatal编程技术网

Asp.net mvc asp.net mvc HTML5日期输入类型

Asp.net mvc asp.net mvc HTML5日期输入类型,asp.net-mvc,html,datepicker,Asp.net Mvc,Html,Datepicker,我在visual studio 2013中使用asp.net mvc,其中一个字段是日期输入,但当在字段中单击并在字段中聚焦时,不会出现自动HTML5日期选择器。如果我手动输入这个字段,它会出现,但是它不会映射到那个特定的字段。在数据注释中,我包含了[DataType(DataType.Date)],但这并没有显示HTML5日期选择器。如何在asp.net中实现这一点下面是该字段的代码 <div class="form-group"> @Html.LabelFor(model

我在visual studio 2013中使用asp.net mvc,其中一个字段是日期输入,但当在字段中单击并在字段中聚焦时,不会出现自动HTML5日期选择器。如果我手动输入这个字段,它会出现,但是它不会映射到那个特定的字段。在数据注释中,我包含了[DataType(DataType.Date)],但这并没有显示HTML5日期选择器。如何在asp.net中实现这一点下面是该字段的代码

<div class="form-group">
    @Html.LabelFor(model => model.CustomerJoinDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.CustomerJoinDate, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.CustomerJoinDate, "", new { @class = "text-danger" })
    </div>
</div>

@LabelFor(model=>model.CustomerJoinDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerJoinDate,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CustomerJoinDate,“,新{@class=“text danger”})

记不清这是什么时候添加的,但在较旧版本的ASP.NET MVC中,您可以修改默认编辑器模板以添加对它的支持,也可以使用
TextBoxFor
来指定任何自定义属性:

@Html.TextBoxFor(model => model.CustomerJoinDate, new { 
    @class = "form-control", 
    type = "date" 
})

请参见Darin关于
input type=“date”
的回答,通过
textbox查看

“…但当在字段中单击并在字段中具有焦点时,自动HTML5日期选择器不会出现”

在底层,您可能需要实现一个插件来跨浏览器工作(例如,等等)

例如:


查看Firefox中是否有日期选择器(从v40.0.2开始):


如果希望DateTime模型属性使用相同的模板,而不必反复重复相同的Razor代码,请在
共享/EditorTemplates
文件夹中定义名为
DateTime.cshtml
(或
DateTime.vbhtml
)的局部视图,其内容类似于:

@model DateTime

@Html.TextBoxFor(
    m => m, 
    "{0:MM/dd/yyyy}", 
    new { @class = "form-control date", type = "date" })
然后将剃须刀绑定替换为:

@Html.EditorFor(model => model.CustomerJoinDate)
依此类推,对于每个应该使用此模板的日期字段

这使用允许您为输入指定的,这有助于“屏蔽”DateTime值的时间部分(假设您只想输入日期)


因为,您可以在本例中使用
date
类根据需要应用polyfill(或者简单地使用
type=date
作为选择器)。

MVC 5.1现在支持直接使用html属性,如下所示。您应该能够将日期输入到attibute类型中

<div class="form-group">
@Html.LabelFor(model => model.CustomerJoinDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    @Html.EditorFor(model => model.CustomerJoinDate, new { htmlAttributes = new { @class = "form-control", type = "date" } })
    @Html.ValidationMessageFor(model => model.CustomerJoinDate, "", new { @class = "text-danger" })
</div>

@LabelFor(model=>model.CustomerJoinDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CustomerJoinDate,new{htmlAttributes=new{@class=“form control”,type=“date”})
@Html.ValidationMessageFor(model=>model.CustomerJoinDate,“,新{@class=“text danger”})

将属性type=“date”添加到htmlattributes括号中如何?感谢各位的建议,我所做的是将type=“date”放在“EditorFor”行的html属性中,如果您想使用EditorFor而不是textboxfor,trickI会在我的回答中的示例中这样做