1

Im using this guide by Stuart Leeks: ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery validate date range validator

I get this "weird" error: "The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'." ...in my create.cshtml view.

Im using a little bit different approach since I am only (for the moment) interested in getting the actual plugin to work. And also I want to be able to use the datepicker when I Create an item.

my "Speaker.cs" model:

[DataType(DataType.Date)]
[DefaultValue("00-00-00")]   <- thought this might fix the "not null error", with no succes..
public DateTime ReleaseDate { get; set;  }

added a partial view "Date.cshtml" under the "views/Speaker"EditorTemplates" folder.

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

my create.cshtml view

@model HiFiWarehouse.Models.Speaker
@Html.EditorFor(model => model.ReleaseDate)

added a "EditorHookup.js" in scripts folder

/// <reference path="jquery-1.5.1.js" />
/// <reference path="jquery-ui-1.8.11.js" />

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

create actions in controller

//
// GET: /Speaker/Create
[Authorize(Roles = "Administrators") 
public ActionResult Create()
{
    return View();
} 

//
// POST: /Speaker/Create

[HttpPost]
[Authorize(Roles = "Administrators")]
public ActionResult Create(Speaker speaker)
{
    if (ModelState.IsValid)
    {
        db.Speakers.Add(speaker);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    return View(speaker);
}

my _Layout.cshtml header:

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/EditorHookup.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>

what am I doing wrong here? :)

1 Answer 1

2

It is telling you that you need to make your ReleaseDate a nullable type:

public DateTime? ReleaseDate { get; set;  }

The other problem is that DefaultValue of 00-00-00 will not work. As far as I am aware DateTime.MinValue is 0001-01-01.

Sign up to request clarification or add additional context in comments.

2 Comments

it didnt fix the problem sorry :(
fixed it! you were nearly correct (ir I missunderstood). The problem was that my partial view didnt accept a null date. @model DateTime?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.