Reputation: 17772
I have a registration page, and because of content issues we have to request and enforce applicants giving a birthdate. So it stands to reason that this field cannot be null.
I am using jQuery to watermark the textbox to tell them that they can click it and get a jQuery UI Calendar object to pick the date. Picking the date works fine, this is not the issue.
In testing, if I try to submit the form without picking the date, I get the following error...
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
I don't want to hardcode a date, the purpose of it being null is to enforce validation so that they have to select it. Any ideas? I'm including my code for the areas responsible. What is really frustrating is that the exception is being thrown before it even reaches the Register(parameters) method. The ModelState.IsValid is never getting called. I've tried try/catch blocks to no avail.
<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;
}
}
Upvotes: 1
Views: 8732
Reputation: 532695
Have the birthdate parameter be a nullable DateTime, i.e., DateTime?, then check if it is null, set a model error, and rerender the view with the error when it is null.
[CaptchaValidator]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(string name, string email, string password, string validation, bool agreement, DateTime? birthdate)
{
ValidateRegistration( name, email, password, validation, agreement, birthdate );
if (ModelState.IsValid)
{
}
return View();
}
private bool ValidateRegistration(string name, string email, string password, string validation, DateTime? birthdate)
{
if (!birthdate.HasValue)
{
this.ModelState.AddModelError( "birthdate", "You must supply a birthdate." );
}
...
Upvotes: 5