1

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.Core.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?

Steve
  • 2,971
  • 7
  • 38
  • 63
  • I also face the same problem after I return a default View(myObject) the default jquery validation rules broken. So i rollback to original View() during Create(). The validation rules come back. Probably there is a bug. The reason I return an object View(myObject) is because it can automatically fill up the default value when call @Html.EditorFor(). – yancyn Oct 15 '13 at 05:00
  • possible duplicate of [ModelState.IsValid == false, why?](http://stackoverflow.com/questions/1791570/modelstate-isvalid-false-why) – Cactus Jul 28 '15 at 01:51

0 Answers0