Nicht nullwertfähiger Typ 'System.DateTime', ASP.NET MVC

Ich habe eine Registrierungsseite und aufgrund von inhaltlichen Problemen müssen wir Antragsteller, die ein Geburtsdatum angeben, auffordern und durchsetzen. Es liegt also nahe, dass dieses Feld nicht null sein kann.

Ich benutze jQuery, um das Textfeld mit einem Wasserzeichen zu versehen und ihnen mitzuteilen, dass sie darauf klicken und ein jQuery-UI-Kalenderobjekt zum Auswählen des Datums erhalten können. Die Auswahl des Datums funktioniert einwandfrei, dies ist nicht das Problem.

Wenn ich beim Testen versuche, das Formular abzusenden, ohne das Datum auszuwählen, wird die folgende Fehlermeldung angezeigt ...

The parameters dictionary contains a null entry for parameter 'birthdate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult Register(System.String, System.String, System.String, System.String, Boolean, System.DateTime)' in 'Controllers.MembershipController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters 

Ich möchte ein Datum nicht fest codieren. Der Zweck, wenn es null ist, besteht darin, die Validierung zu erzwingen, damit sie es auswählen müssen. Irgendwelche Ideen? Ich füge meinen Code für die verantwortlichen Bereiche hinzu. Was wirklich frustrierend ist, ist, dass die Ausnahme ausgelöst wird, bevor sie überhaupt die Register (parameters) -Methode erreicht. Das ModelState.IsValid wird nie aufgerufen. Ich habe versucht, Blöcke ohne Erfolg zu versuchen / zu fangen.

        <p>             
            <label for="birthday">Birthdate:</label><br />
            <%= Html.TextBox("birthdate", "Select month, year, and date last." , new { @class = "text watermarkOn", @tabindex = "5" }) %>
        </p>

    public ActionResult Register()
    {
        return View();
    } 

    [CaptchaValidator]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Register(string name, string email, string password, string validation, bool agreement, DateTime birthdate)
    {

        // attempt to validate the registration state, and if it is invalid, 
        // populate the ruleviolations and redisplay the content with the errors.
        if (ModelState.IsValid)
        {
        }

        return View();
    }

    private bool ValidateRegistration(string name, string email, string password, string validation, DateTime birthdate)
    {
        if (String.IsNullOrEmpty(name))
        {
            ModelState.AddModelError("name", "You must specify a name.");
        }
        if (String.IsNullOrEmpty(email))
        {
            ModelState.AddModelError("email", "You must specify an email address.");
        }
        if (password == null || !Text.RegularExpressions.Password(password) )
        {
            ModelState.AddModelError("password",
                String.Format(System.Globalization.CultureInfo.CurrentCulture,
                     "You must specify a password of {0} or more characters, without any whitespace characters.",6));
        }
        if (!String.Equals(password, validation, StringComparison.Ordinal))
        {
            ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
        }
        return ModelState.IsValid;
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage