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" } )
}
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."}
Thanks for any help,
Mark
You haven't specified a
DateTime
in theString.Format
- that is why you are getting that error, it expects one parameter, but you haven't supplied any. Try usingDateTime.Now
e.g.
Alternatively, just add two
DateTime
properties to your ViewModel, and use theEditorFor
helper on them.Don't use
TextBox
inside your main view. If you want your custom editor template to render you should use theEditorFor
helper:If the
From
andTo
properties are of typeDateTime
, then ASP.NET MVC will wutomatically render your custom editor template (~/Views/Shared/EditorTemplates/DateTime.cshtml
).