Note that there are some explanatory texts on larger screens.

plurals
  1. POusing DataAnnotations to validate Model, not working
    primarykey
    data
    text
    <p>I have this code on an Entity created using EF 4:</p> <pre><code>[MetadataType(typeof(MemberMetaData))] public partial class Member { public string ConfirmPassword { get; set; } [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "Password and confirm password do not match.")] public class MemberMetaData { [Required(ErrorMessage = "Email is required.")] [DataType(DataType.EmailAddress, ErrorMessage = "Email is invalid.")] //[RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$")] public object Email { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("Password")] public object Password { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("Confirm Password")] public object ConfirmPassword { get; set; } } } </code></pre> <p>Members properties in the Member entity are Email and Password so far.</p> <p>I'm trying to use the data annotations to return a validation message back to the view and have created a unit test like so:</p> <pre><code> [TestMethod] public void register_post_displays_error_when_email_not_entered() { // Arrange Member model = new Member() { Email = "", Password = "test123", ConfirmPassword = "test123" }; // Act var result = _controller.Register(model) as ViewResult; // Assert Assert.AreEqual("Email is required.", result.ViewData.ModelState["Email"].Errors[0].ErrorMessage); } </code></pre> <p>This is failing, when I step into the controller ModelState.IsValid is always true. Here's my controller action:</p> <pre><code> [HttpPost] public ActionResult Register(Member model) { if (ModelState.IsValid) { //register } // If we got this far, something failed, redisplay form return View("Register"); } </code></pre> <p>Why is this not working?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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