2011-06-30 44 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" }) 
相關問題