添加使用日期时间模板的搜索表单 [英] Adding a search form using a DateTime template
问题描述
在我EditorTemplates,我有DateTime.cshtml - 这工作找到创建/编辑/更新的观点:
In my EditorTemplates, I have DateTime.cshtml - which works find in create/edit/update views:
@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" } )
}
在创建搜索的看法,我也想用一个日期时间选择器 - ?我怎么会code中的视图中使用code以上时,它没有连接到一个模式,但只是普通的HTML
When creating a search view, I also want to use a datetime picker - how would I code the view use the code above, when it's not linked to a model, but just plain HTML?
如果我只需要输入以下到我的剃须刀标记:
If I just enter the following into my Razor markup:
@using (Html.BeginForm())
{
<p>
Availability between: @Html.TextBox( "From" , String.Format( "{0:dd/MM/yyyy}") , new { @class = "datepicker span2" } )
and: @Html.TextBox( "To" , String.Format( "{0:dd/MM/yyyy}") , new { @class = "datepicker span2" } )
<input type="submit" value="Search" /></p>
}
我刚刚得到的错误:
I just get the error:
{"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."}
感谢您的帮助,
标记
推荐答案
不要使用文本框
主视图中。如果你希望你的自定义编辑器模板来渲染,你应该使用 EditorFor
助手:
Don't use TextBox
inside your main view. If you want your custom editor template to render you should use the EditorFor
helper:
@using (Html.BeginForm())
{
<p>
Availability between:
@Html.EditorFor(x => x.From)
and:
@Html.EditorFor(x => x.To)
<input type="submit" value="Search" />
</p>
}
如果在从
和到
属性类型的DateTime
,然后ASP.NET MVC将wutomatically呈现您的自定义编辑模板(〜/查看/共享/ EditorTemplates / DateTime.cshtml
)。
If the From
and To
properties are of type DateTime
, then ASP.NET MVC will wutomatically render your custom editor template (~/Views/Shared/EditorTemplates/DateTime.cshtml
).
这篇关于添加使用日期时间模板的搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!