Guest User

Untitled

a guest
Apr 1st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. public sealed class UserModel
  2. {
  3. [Key]
  4. [Required]
  5. [StringLength(20, MinimumLength = 4)]
  6. public string Username { get; set; }
  7.  
  8. [Required]
  9. [EmailAddress]
  10. [DataType(DataType.EmailAddress)]
  11. public string Email { get; set; }
  12.  
  13. [Required]
  14. [DataType(DataType.Password)]
  15. [StringLength(20, MinimumLength = 4)]
  16. public string Password { get; set; }
  17.  
  18. public bool IsValid { get; set; }
  19. }
  20.  
  21. public class UserViewModel {
  22. [Required]
  23. [StringLength(20, MinimumLength = 4)]
  24. public string Username { get; set; }
  25.  
  26. [Required]
  27. [EmailAddress]
  28. [DataType(DataType.EmailAddress)]
  29. public string Email { get; set; }
  30.  
  31. [Required]
  32. [DataType(DataType.Password)]
  33. [StringLength(20, MinimumLength = 4)]
  34. public string Password { get; set; }
  35. }
  36.  
  37. public ActionResult Post([FromBody] UserViewModel viewModel) {
  38. if(ModelState.IsValid) {
  39. //create new model
  40. var model = new UserModel {
  41. Username = viewModel.Username,
  42. Email = viewModel.Email,
  43. Password = viewModel.Password
  44. };
  45.  
  46. //or model retrieved from data storage
  47.  
  48. //...some other code...
  49.  
  50. model.IsValid = true; //only the server can modify this value
  51.  
  52. //...
  53. }
  54.  
  55. //...
  56. }
Add Comment
Please, Sign In to add comment