本文介绍了非可空类型'System.DateTime的',ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册页面,因为内容问题,我们必须要求和执行申请人给予生日。所以,按理说,这个字段不能为空。

我使用jQuery水印文本框告诉他们,他们可以单击它,并得到一个jQuery UI Calendar对象挑选日期。采摘日期做工精细,这不是问题。

在测试中,如果我尝试提交表单没有采摘日期,我得到以下错误...

 参数词典共包含参数法System.Web.Mvc.ActionResult注册非可空类型'System.DateTime的'的'生日'空条目(System.String, System.String,System.String,System.String,布尔值的System.DateTime)中的Controllers.MembershipController。为了使参数可选其类型应该是引用类型或可空类型。
参数名:参数

我不想硬code的日期,它是空的目的是为了执行验证,使他们不得不选择它。有任何想法吗?我包括我的code负责的区域。什么是真正令人沮丧的是,异常被抛出甚至达到寄存器(参数)方法之前。该ModelState.IsValid永远不会获取调用。我试过try / catch块无济于事。

 < P>
<标签=生日>生日:< /标签>< BR />
<%= Html.TextBox(生日,选择月,年和日期最后一次。新的{@class =TEXT watermarkOn,@tabindex =5})%GT;
&所述; / P>    公众的ActionResult寄存器()
    {
        返回查看();
    }    [CaptchaValidator]
    的[AcceptVerbs(HttpVerbs.Post)
    公众的ActionResult寄存器(字符串名称,字符串email,字符串密码,串验证,布尔协议,日期的生日)
    {        //尝试验证登记状态,如果它是无效的,
        //填充ruleviolations并重新与错误的内容。
        如果(ModelState.IsValid)
        {
        }        返回查看();
    }    私人布尔ValidateRegistration(字符串名称,字符串email,字符串密码,串验证,DateTime的生日)
    {
        如果(String.IsNullOrEmpty(名))
        {
            ModelState.AddModelError(名,你必须指定一个名称。);
        }
        如果(String.IsNullOrEmpty(电子邮件))
        {
            ModelState.AddModelError(电子邮件,您必须指定一个电子邮件地址。);
        }
        如果(密码== NULL ||!Text.RegularEx pressions.Password(密码))
        {
            ModelState.AddModelError(密码
                的String.Format(System.Globalization.CultureInfo.CurrentCulture,
                     你必须指定{0}或多个字符的密码,没有任何空白字符。6));
        }
        如果(!String.Equals(密码,验证StringComparison.Ordinal))
        {
            ModelState.AddModelError(_表,新密码和确认密码不匹配。);
        }
        返回ModelState.IsValid;
    }
}


解决方案

有生日参数是一个为空的日期时间,也就是说,日期时间?,然后检查是否为空,设置一个模型误差,并重新呈现有错误的看法当它为null。

  [CaptchaValidator]
的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult寄存器(字符串名称,字符串email,字符串密码,串验证,布尔协议,日期时间?生日)
{
    ValidateRegistration(姓名,电子邮件,密码,验证,协议,出生日期);
    如果(ModelState.IsValid)
    {
    }
    返回查看();
}私人布尔ValidateRegistration(字符串名称,字符串email,字符串密码,串验证,DateTime的?生日)
{
     如果(!birthdate.HasValue)
     {
         this.ModelState.AddModelError(生日,你必须提供一个生日。);
     }     ...

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;
    }
}
解决方案

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

     ...

这篇关于非可空类型'System.DateTime的',ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:48