tipo não anulável 'System.DateTime', ASP.NET MVC
Tenho uma página de registro e, devido a problemas de conteúdo, temos que solicitar e impor aos solicitantes uma data de nascimento. Portanto, é lógico que esse campo não pode ser nulo.
Eu estou usando o jQuery para watermark a caixa de texto para dizer-lhes que eles podem clicar nele e obter um objeto de calendário da interface do usuário do jQuery para escolher a data. Escolher a data funciona bem, esse não é o problema.
Nos testes, se eu tentar enviar o formulário sem escolher a data, recebo o seguinte erro ...
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
Eu não quero codificar uma data, a finalidade de ser nula é impor a validação para que eles tenham que selecioná-la. Alguma ideia? Estou incluindo meu código para as áreas responsáveis. O que é realmente frustrante é que a exceção está sendo lançada antes mesmo de chegar ao método Register (parameters). O ModelState.IsValid nunca é chamado. Eu tentei tentar / catch blocos sem sucesso.
<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;
}
}