{"id":227,"date":"2021-03-30T19:03:51","date_gmt":"2021-03-30T10:03:51","guid":{"rendered":"https:\/\/mvc.auctionpro.co.kr\/?page_id=227"},"modified":"2021-03-30T19:03:51","modified_gmt":"2021-03-30T10:03:51","slug":"modelstate-isvalid","status":"publish","type":"page","link":"https:\/\/mvc.auctionpro.co.kr\/?page_id=227","title":{"rendered":"ModelState.IsValid"},"content":{"rendered":"<h3>Remark : <\/h3>\n<h4> ModelState.IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. <\/h4>\n<pre class=\"lang:default decode:true \" >public class Encaissement : IValidatableObject\r\n{\r\n    \/\/ A required attribute, validates that this value was submitted    \r\n    [Required(ErrorMessage = \"The Encaissment ID must be submitted\")]\r\n    public int EncaissementID { get; set; }\r\n\r\n    public DateTime? DateEncaissement { get; set; }\r\n\r\n    public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)\r\n    {\r\n        var results = new List&lt;ValidationResult&gt;();\r\n\r\n        \/\/ Validate the DateEncaissment\r\n        if (!this.DateEncaissement.HasValue)\r\n        {\r\n            results.Add(new ValidationResult(\"The DateEncaissement must be set\", new string[] { \"DateEncaissement\" });\r\n        }\r\n\r\n       return results;\r\n    }\r\n}<\/pre>\n<pre class=\"lang:default decode:true \" >[HttpPost]\r\n[ValidateAntiForgeryToken]\r\npublic ActionResult Create([Bind(Include = \"EncaissementID,libelle,DateEncaissement,Montant,ProjetID,Description\")] Encaissement encaissement) {\r\n\r\n  \/\/ Perform validation\r\n  if (!encaissement.DateEncaissement.HasValue)\r\n  {\r\n      this.ModelState.AddModelError(\"DateEncaissement\", \"The DateEncaissement must be set\");\r\n  }\r\n\r\n  encaissement.Montant = Convert.ToDecimal(encaissement.Montant);\r\n\r\n  ViewBag.montant = encaissement.Montant;\r\n\r\n  if (ModelState.IsValid) {\r\n\r\n    db.Encaissements.Add(encaissement);\r\n    db.SaveChanges();\r\n    return RedirectToAction(\"Index\", \"Encaissement\");\r\n\r\n  };\r\n\r\n  ViewBag.ProjetID = new SelectList(db.Projets, \"ProjetId\", \"nomP\");\r\n\r\n  return View(encaissement);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remark : ModelState.IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. public class Encaissement : IValidatableObject { \/\/ A required attribute, validates that this value was submitted [Required(ErrorMessage = &#8220;The Encaissment ID\u2026 <span class=\"read-more\"><a href=\"https:\/\/mvc.auctionpro.co.kr\/?page_id=227\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-227","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=227"}],"version-history":[{"count":1,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/227\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/227\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}