【问题标题】:MVC jquery datepicker for create allow nulls用于创建的 MVC jquery datepicker 允许空值
【发布时间】:2011-06-30 02:23:38
【问题描述】:

我正在查看此链接:

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 datepicker 以在编辑中选择日期。

本教程工作正常,但仅适用于编辑而不是创建。

它有一个模型:

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"});
});

对于任何带有 .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'.

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

【问题讨论】:

    标签: c# jquery asp.net-mvc asp.net-mvc-3 jquery-ui-datepicker


    【解决方案1】:

    类似:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多