MVC jquery datepicker for create allow nulls
I was checking out this link:
http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx
which describes creating a jquery datepicker for MVC for date selections in edits.
Sure the tutorial works fine but only works for edits and not creates.
It has a model of:
public class Foo
{
public string Name { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
}
I modified its edit to create a create like:
@model DatePickerTemp.Models.FooEditModel @{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>
Create<开发者_JAVA技巧;/h2>
@Model.Message
@using (Html.BeginForm())
{
@Html.EditorFor(m => m.Foo)
<input id="submit" name="submit" type="submit" value="Save" />
}
}
The other code uses some jquery:
$(document).ready(function () {
$('.date').datepicker({dateFormat: "dd/mm/yy"});
});
for anything with class .date:
There is another piece of code:
@model DateTime
@Html.TextBox("", Model.ToString("dd/MM/yyyy"), new { @class = "date" })
which sets the class on the objects of type:
[DataType(DataType.Date)]
to create a jquery datepicker.
The problem is this type is a non nullable type so when you run the create you get:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
Does anyone know how to modify the code to make the create work?
Something like:
@model DateTime?
@Html.TextBox("", Model.HasValue && Model.Value != DateTime.MinValue ? Model.Value.ToString("dd MMM yyyy") : string.Empty, new { @class = "datePicker" })
精彩评论