{"id":495,"date":"2019-02-23T21:34:54","date_gmt":"2019-02-24T01:34:54","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=495"},"modified":"2019-02-23T21:37:35","modified_gmt":"2019-02-24T01:37:35","slug":"display-custom-input-error-messages-in-an-mvc-view-in-asp-net","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/display-custom-input-error-messages-in-an-mvc-view-in-asp-net\/","title":{"rendered":"Display Custom Input Error Messages in an MVC View in ASP.Net"},"content":{"rendered":"<p>It is easy to display input errors using a field's validation message in a view. Simply call <code>ModelState.AddModelError()<\/code> passing in the property ID and error message, then return the View(). It is a great way to tell the user about an issue with their input while keeping them on the same page.<\/p>\n<p>From the controller's Edit() function:<\/p>\n<pre><code>if (NicknameAlreadyExists(SomeNickname)) {\n    ModelState.AddModelError(\"Nickname\", \"Nickname already in use.\");\n    return View();\n}\n<\/code><\/pre>\n<p>In the view:<\/p>\n<pre><code>@Html.LabelFor(model =&gt; model.Nickname, htmlAttributes: new { @class = \"control-label col-md-2\" })\n@Html.EditorFor(model =&gt; model.Nickname, new { htmlAttributes = new { @class = \"form-control\" } })\n@Html.ValidationMessageFor(model =&gt; model.Nickname, \"\", new { @class = \"text-danger\" })\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It is easy to display input errors using a field&#8217;s validation message in a view. Simply call ModelState.AddModelError() passing in the property ID and error message, then return the View().  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[22],"tags":[],"class_list":["post-495","post","type-post","status-publish","format-standard","hentry","category-mvc"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-7Z","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":4,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/495\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}