Note that there are some explanatory texts on larger screens.

plurals
  1. POModelState.IsValid fails to validate!
    text
    copied!<p>So I have a simple class which I use to send a Contact Form email. </p> <pre><code>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; } } </code></pre> <p>The above is my class and my controller is as follows </p> <pre><code>[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 } }; } </code></pre> <p>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.</p> <p>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!</p> <p>Either way its not validating correctly. Any Ideas what it could be?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload