Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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 使用日期时间模板添加搜索表单_Asp.net_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net 使用日期时间模板添加搜索表单

Asp.net 使用日期时间模板添加搜索表单,asp.net,asp.net-mvc,asp.net-mvc-3,Asp.net,Asp.net Mvc,Asp.net Mvc 3,在我的EditorTemplates中,我有DateTime.cshtml,它可以在创建/编辑/更新视图中找到: @model Nullable<System.DateTime> @if ( Model.HasValue ) { @Html.TextBox( "" , String.Format( "{0:dd/MM/yyyy}" , Model.Value ) , new { @class = "datepicker span2" } ) } else {

在我的EditorTemplates中,我有DateTime.cshtml,它可以在创建/编辑/更新视图中找到:

@model Nullable<System.DateTime> 

@if ( Model.HasValue ) { 
   @Html.TextBox( "" , String.Format( "{0:dd/MM/yyyy}" , Model.Value ) , new  { @class = "datepicker span2" } ) 
} 
else { 
   @Html.TextBox( "" , String.Format( "{0:dd/MM/yyyy}" , DateTime.Now ) , new { @class = "datepicker span2" } ) 
} 
谢谢你的帮助


标记

您没有在
字符串.Format
中指定
日期时间
-这就是为什么会出现错误,它需要一个参数,但您没有提供任何参数。尝试使用
DateTime.Now

e、 g


或者,只需将两个
DateTime
属性添加到ViewModel中,并在其上使用
EditorFor
帮助程序。

不要在主视图中使用
TextBox
。如果希望自定义编辑器模板呈现,应使用
编辑器for
帮助程序:

@using (Html.BeginForm())
{
    <p>
        Availability between: 
        @Html.EditorFor(x => x.From)
        and: 
        @Html.EditorFor(x => x.To)

        <input type="submit" value="Search" />
    </p>
}
@使用(Html.BeginForm())
{

在以下地点之间的可用性:
@EditorFor(x=>x.From)
以及:
@EditorFor(x=>x.To)

}

如果
From
To
属性的类型为
DateTime
,则ASP.NET MVC将自动呈现您的自定义编辑器模板(
~/Views/Shared/EditorTemplates/DateTime.cshtml

Hi@Darin Dimitrov-From和To不是房间模型的属性(在房间控制器内,将上述视图表单附加到其上),而不是链接到房间模型的链接表的属性-但是,这些属性仅在控制器内链接,因此我不确定从何处声明从/到,以便将它们作为DateTime提取。谢谢,Mark不知道您在说什么。您应该将一个视图模型传递到包含这些属性的视图。ASP.NET MVC视图我们不了解EF模型、表、链接表和其他内容。ASP.NET MVC视图操作视图模型,该视图模型是您专门为视图要求定义的类。但是,如果这些日期时间在模型的某些复杂属性中,您可以始终在lambda表达式中指定:
@Html.EditorFor(x=>x.SomeProperty.To)
.ViewModel是我一直在寻找的(我一直挂在只连接到数据库表的模型上)-我知道我以前见过与此相关的东西…谢谢,马克
{"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."}
@Html.TextBox( "From" , String.Format( "{0:dd/MM/yyyy}", DateTime.Now ), 
new  { @class = "datepicker span2" } ) 
@using (Html.BeginForm())
{
    <p>
        Availability between: 
        @Html.EditorFor(x => x.From)
        and: 
        @Html.EditorFor(x => x.To)

        <input type="submit" value="Search" />
    </p>
}