title: How to add validation errors in the validation collection asp.net mvc?
body: <p>Inside my controller's action I have the following code: </p>
  
  <pre><code>public ActionResult GridAction(string id)
  {
      if (String.IsNullOrEmpty(id)) 
      {
          // add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
      }
  
      return View(); 
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <pre><code>if (String.IsNullOrEmpty(id))
  {
      // add error 
      ModelState.AddModelError("GridActionDropDownList", "Please select an option");
      return RedirectToAction("Orders"); 
  }
  </code></pre>
  
  <p><strong>UPDATE 2:</strong> </p>
  
  <p>Here is my updated code: </p>
  
  <pre><code>@Html.DropDownListFor(x =&gt; x.SelectedGridAction, Model.GridActions,"Please Select") 
  @Html.ValidationMessageFor(x =&gt; x.SelectedGridAction)  
  </code></pre>
  
  <p>The Model looks like the following: </p>
  
  <pre><code>public class MyInvoicesViewModel
  {
  
      private List&lt;SelectListItem&gt; _gridActions;
  
      public int CurrentGridAction { get; set; }
  
      [Required(ErrorMessage = "Please select an option")]
      public string SelectedGridAction { get; set; }
  
      public List&lt;SelectListItem&gt; GridActions
      {
          get
          {
              _gridActions = new List&lt;SelectListItem&gt;();
              _gridActions.Add(new SelectListItem() { Text = "Export to Excel", Value = "1" });
  
              return _gridActions;
          }
      }
  } 
  </code></pre>
  
  <p>And here is my controller action: </p>
  
  <pre><code>public ActionResult GridAction(string id)
  {
      if (String.IsNullOrEmpty(id))
      {
          // add error 
          ModelState.AddModelError("SelectedGridAction", "Please select an option");
          return RedirectToAction("Orders"); 
      }
  
      return View(); 
  }
  </code></pre>
  
  <p>Nothing happens! I am totally lost on this one! </p>
  
  <p>UPDATE 3: </p>
  
  <p>I am now using the following code but still the validation is not firing: </p>
  
  <pre><code>public ActionResult GridAction(string id)
  {
      var myViewModel= new MyViewModel();
      myViewModel.SelectedGridAction = id; // id is passed as null           
  
      if (!ModelState.IsValid)
      {
          return View("Orders");
      }
  </code></pre>
  
  <p>UPDATE 4: </p>
  
  <pre><code>$("#linkGridAction").click(function () {
      alert('link grid action clicked'); 
  
      $.get('GridAction/', { SelectedGridAction: $("#SelectedGridAction").val() }, function (result) {
          alert('success');
      });
  });
  </code></pre>
  
  <p>And the Controller looks like the following: </p>
  
  <pre><code>// OrderViewModel has a property called SelectedGridAction. 
  public ActionResult GridAction(OrderViewModel orderViewModel)
  {
      return View(); 
  }
  </code></pre>
  
  <p>UPDATE 5: Validation is not firing: </p>
  
  <pre><code>public ActionResult GridAction(OrderViewModel orderViewModel)
  {
      if (!ModelState.IsValid)
      {
          return View("Orders", orderViewModel); 
      }
      return View(); 
  }
  </code></pre>