ModelState.IsValid无法验证!

时间:2011-05-05 21:10:37

标签: javascript model-view-controller modelstate model-validation

所以我有一个简单的课程,用于发送联系表格电子邮件。

public class EnquiryEmail : Entity
{
    [DataType(DataType.Text)]
    [Required(ErrorMessage = "Name is a required field")]
    [StringLength(100, ErrorMessage = "Must be under 100 characters")]
    public virtual string Name { get; set; }

    [DisplayName("Tel")]
    [DataType(DataType.PhoneNumber)]
    [StringLength(20, ErrorMessage = "Must be under 20 characters")]
    public virtual string Tel { get; set; }


    [DataType(DataType.EmailAddress)]
    [Required(ErrorMessage = "Email is a required field")]
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")]
    [StringLength(100, ErrorMessage = "Must be under 100 characters")]
    public virtual string Email { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "Enquiry is a required field")]
    public virtual string Enquiry { get; set; }

}

以上是我的班级,我的控制器如下

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Enquiry(EnquiryEmail model)
    {
        if (ModelState.IsValid)
        {
            //Send a Email to Admin!!
            if (ForSale.Core.Email.EnquiryEmail(model))
            {
                return new JsonResult { Data = new { Success = true } };
            }
        }

        return new JsonResult { Data = new { Success = false } };
    }

由于某种原因,ModelState.IsValid返回true,即使我可以查看我的EnquiryEmail对象,并查看针对对它们进行“必需”验证的值的空值。

查看ModelState.IsValid显示4个值 - 在名为“AttemptedValues”的属性下,Null实际显示为空字符串。我不确定这是一个问题,还是仅仅是ModeState的事情!

无论哪种方式都没有正确验证。任何想法可能是什么?

0 个答案:

没有答案