title: How to Validate with ASP.NET MVC - Model State is Invalid When Form Submitted
body: <p>I am trying to validate a form in MVC. </p>
  
  <p>I add custom errors to model state and get it as invalid when form is submitted. When the view is displayed it doesn’t show validation messages nor validation summary. Can anyone please let me know what am I doing wrong or point me in the right direction if there is any other way of validating? </p>
  
  <p><strong>Edit</strong> This is ASP.NET MVC 1. Here's the code:</p>
  
  <p>Following is the entity</p>
  
  <pre><code>namespace DCS.DAL.Entities
  {
      public class Group : IDataErrorInfo
      {
          public int GroupId { get; set; }
  
          public string GroupName { get ; set; }
          public string AboutText { get; set; }
          public string LogoURL { get; set; }
          public string FriendlyURL { get; set; }
          public bool ExcludeFromFT { get; set; }
          public ContactInfo ContactInfo { get; set; }
  
          public string Error { get { return string.Empty;  } }
  
          public string this[string propName]
          {
              get
              {
                  if ((propName == "GroupName") &amp;&amp; string.IsNullOrEmpty(GroupName))
                      return "Please enter Group Name";
                  return null;
              }
          }
  
  
      }
  }
  </code></pre>
  
  <p>Following is the view </p>
  
  <pre><code>&lt;%= Html.ValidationSummary("Please correct following details") %&gt;  
  
      &lt;% using (Html.BeginForm()) {%&gt;
  
      &lt;div id="divError" Style="display:none;"&gt;
          errors 
          &lt;%
                  foreach (KeyValuePair&lt;string, ModelState&gt; keyValuePair in ViewData.ModelState)
                  {
                      foreach (ModelError modelError in keyValuePair.Value.Errors)
                      {
              %&gt; 
                           &lt;% Response.Write(modelError.ErrorMessage); %&gt;
              &lt;%
                      }
                  }
              %&gt;   
      &lt;/div&gt;
  
          &lt;fieldset&gt;
  
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;label for="GroupName"&gt;Group Name:&lt;/label&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;%= Html.TextBox("GroupName", Model.GroupName) %&gt;
                          &lt;%= Html.ValidationMessage("GroupName","group") %&gt;
                      &lt;/td&gt;
  </code></pre>
  
  <p>Foreach loop is for testing, it does gets into the for loop but doesn’t response.write error message nor validation summary nor validation message. </p>
  
  <p>Following is the controller</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Post)]
          public ActionResult EditGroup(Group group, FormCollection collection)
          {
              //Group group = new Group();
              bool success = false;
              try
              {
  
                      var contactInfo = new ContactInfo
                                            {
  
                                                ContactName = collection["ContactName"],
                                                Email = collection["Email"],
                                                Fax = collection["Fax"],
                                                HeadOfficeAddress = collection["HeadOfficeAddress"],
                                                Freephone = collection["Freephone"],
                                                Telephone = collection["Telephone"],
                                                Website = collection["Website"]
                                            };
  
                  group.ContactInfo = contactInfo;
                  group.GroupName = collection["GroupName"];
                  if(string.IsNullOrEmpty(group.GroupName))
                  {
                      ModelState.AddModelError("GroupName", "Please enter group name");
  
                  }
  
                  if (!ModelState.IsValid)
                  {
                      success = groupRepository.InsertUpdateGroup(group);
                      return View(group);
                  }
  
              }
              catch
              {
  
              }
  
              //return Json(success);
              return View(group);
          }
  </code></pre>
  
  <p>It does go into the <code>if(!Modelstate.isvalid)</code> loop but it doesn’t display error. </p>
  
  <p><strong>Edit 2</strong> I can see in the Text Visualiser that the Validation Summary does have the error message, it wont display on screen though.</p>
  
  <p>Thanks</p>