0

I'm still very new at MVC so bear with me here. Would the following be acceptable for validation since both Email and Created are being validated in the Model and the ModelView is creating an instance of that same User model?...

Or do you need to explicitly have the same fields called out in both the Model and ModelView?

   //Model Class
   public class User
   {
       [Required]
       public string Email {get; set;}

       [Required]
       public DateTime Created {get; set;}
   }

    //ViewModel Class
    public class UserViewModel
    {
        public User user {get; set;}
    }
vercingortix
  • 199
  • 2
  • 15
  • [Required] attribute is validating UserModel UserViewModel does not do anything. ModelState.IsValid checks the validation if there's any error. You can check this link http://stackoverflow.com/questions/881281/what-is-modelstate-isvalid-valid-for-in-asp-net-mvc-in-nerddinner – Miguel Nov 11 '16 at 19:45
  • Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. –  Nov 11 '16 at 21:50
  • View models should never contain properties which are data models when editing. –  Nov 11 '16 at 21:51

1 Answers1

0

if User is your entity of domain class its better not use it in your model . define the field of your User class in your model again and do validation for it.although your validation must work in your code.

Salar Afshar
  • 229
  • 1
  • 2
  • 13