Guest User

Untitled

a guest
Jul 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public ActionResult Index()
  2. {
  3. User u = new User();
  4. u.Username = "test";
  5. u.Password = "test";
  6. u.Email = "test";
  7.  
  8. DefaultContext db = new DefaultContext();
  9.  
  10. if (ModelState.IsValid)
  11. {
  12. db.Users.Add(u);
  13. db.SaveChanges();
  14. Response.Write(u.Id);
  15. }
  16. else
  17. // model is not valid
  18.  
  19. return View();
  20. }
  21.  
  22. public class Widget
  23. {
  24. public int Id { get; set; }
  25.  
  26. [Required]
  27. public string Name { get; set; }
  28.  
  29. public decimal Price { get; set; }
  30. }
  31.  
  32. var widget = new Widget
  33. {
  34. Id = 12,
  35. Price = 15.57M
  36. };
  37.  
  38. var context = new ValidationContext(widget, null, null);
  39. var results = new List<ValidationResult>();
  40. if( Validator.TryValidateObject( widget, context, results, true ) )
  41. {
  42. //Validation Successful
  43. }
  44. else
  45. {
  46. //Validation Failed
  47. }
  48.  
  49. User u = new User();
  50. u.Username = "test";
  51. u.Password = "test";
  52. u.Email = "test";
  53.  
  54.  
  55. if (TryValidateModel(u))
  56. {
  57.  
  58. }else{
  59.  
  60. }
Add Comment
Please, Sign In to add comment