2011-06-30 43 views
1

我被检查出此链接:MVC jQuery的日期选择器用于创建允许空值

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

其描述创建MVC一个jQuery日期选择器中编辑选择日期。

当然,教程工作正常,但只适用于编辑,而不是创建。

它有一个模型:

public class Foo 
    { 
     public string Name { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime Date { get; set; } 
    } 

我修改了它的编辑来创建一个创造,如:

@model DatePickerTemp.Models.FooEditModel @{ 
    ViewBag.Title = "Create"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; } <h2> 
    Create</h2> 
@Model.Message 
@using (Html.BeginForm()) 
{ 
    @Html.EditorFor(m => m.Foo) 
    <input id="submit" name="submit" type="submit" value="Save" /> 
} 

}

其他代码使用一些jQuery的:

$(document).ready(function() { 
    $('.date').datepicker({dateFormat: "dd/mm/yy"}); 
}); 

for anythin克与类.date:

还有另一段代码:

@model DateTime 
@Html.TextBox("", Model.ToString("dd/MM/yyyy"), new { @class = "date" }) 

它设置于类型的对象类:

[DataType(DataType.Date)] 

创建一个jquery日期选择器。

问题是这样的类型为非可空类型,所以当你运行创造你:

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'. 

有谁知道如何修改代码,以使创建工作?

回答

2

喜欢的东西:

@model DateTime? 
    @Html.TextBox("", Model.HasValue && Model.Value != DateTime.MinValue ? Model.Value.ToString("dd MMM yyyy") : string.Empty, new { @class = "datePicker" }) 
相关问题