我有一个注册页面,由于内容问题,我们必须要求并强制申请人提供出生日期。所以按理说这个字段不能为空。
我使用 jQuery 在文本框上加水印,告诉他们可以单击它并获取 jQuery UI 日历对象来选择日期。选择日期效果很好,这不是问题。
在测试中,如果我尝试在不选择日期的情况下提交表单,则会收到以下错误...
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
我不想对日期进行硬编码,它为空的目的是强制验证,以便他们必须选择它。有任何想法吗?我包括了负责区域的代码。真正令人沮丧的是,异常在到达 Register(parameters) 方法之前就被抛出了。 ModelState.IsValid 永远不会被调用。我尝试过 try/catch 块但无济于事。
<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;
}
}