2009-10-30 59 views
1

我有一个注册页面,并且由于内容问题,我们必须请求和执行给出生日的申请人。所以它坚持认为这个字段不能为空。非空类型'System.DateTime',ASP.NET MVC

我正在使用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(参数)方法之前抛出异常。 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; 
    } 
} 

回答

5

具有出生日期参数是可为空的日期时间,即,日期时间?,然后检查它是否为空,设置一个模型错误,并且重新呈现有错误的视图时,它为空。

[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."); 
    } 

    ... 
+0

这绝对是完美的。非常感谢。 – Ciel 2009-10-30 16:58:38

相关问题