So I have a simple class which I use to send a Contact Form email.
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; }
}
The above is my class and my controller is as follows
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Enquiry(EnquiryEmail model)
{
if (ModelState.IsValid)
{
//Send a Email to Admin!!
if (ForSale.Co开发者_如何学Cre.Email.EnquiryEmail(model))
{
return new JsonResult { Data = new { Success = true } };
}
}
return new JsonResult { Data = new { Success = false } };
}
For some reason the ModelState.IsValid returns as true even though I can look into my EnquiryEmail object and see the nulls against values which have a "Required" validation against them.
Look into the ModelState.IsValid shows the 4 values -the Nulls are actually displayed as empty strings under the property named "AttemptedValues". which im not sure if thats a problem here or just a thing ModeState does!
Either way its not validating correctly. Any Ideas what it could be?
精彩评论