title: How To Validate DropDown List in Asp .net MVC
body: <p>The List of the month are Already added In Databases. For Adding the salary (Create operation), i have to select the month type from the Drop Down List, if the month is not selected the program should not have to redirect to create action. How Can i validate the Drop Down, Before Routing to Create Action ? </p>
  
  <pre><code>@using(Html.BeginForm("Create","Talab",FormMethod.Post))
  { 
      &lt;div class="row"&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-2"&gt;
                  &lt;a href="@Url.Action("Create","TotalSalary")" class="btn btn-success input-sm"&gt;Add New &lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;   
              &lt;div class="col-md-2"&gt;
                  @Html.DropDownListFor(model =&gt; model.Month.id,     (IEnumerable&lt;SelectListItem&gt;)ViewData["monthType"], "--Select a Month--")
                  @Html.ValidationMessageFor(model =&gt; model.Month.id)
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>My View Model has following Property</p>
  
  <pre><code>public class Salary
  {
     public int id { get; set; }
     public Nullable&lt;int&gt; month_id { get; set; }
  
     [Required]
     public virtual Month Month { get; set; }
  
     public IEnumerable&lt;Month&gt; GetMonths()
     {
          IEnumerable&lt;Month&gt; mat = null;
          mat = this.db.Months.ToList();
          return mat;
     }
  
  }
  Public Class Month
  {
      public int id { get; set; }
      public string month { get; set; }
      public virtual ICollection&lt;Salary&gt; Salary { get; set; }
  }
  </code></pre>
  
  <p>My Controller Action Index </p>
  
  <pre><code>public ActionResult Index()
      {
          Salary salary = new Salary();
          ViewData["monthType"] = salary .GetMonths().ToList().Select(
                   s =&gt; new SelectListItem
                   {
                       Text = s.month,
                       Value = s.id.ToString()
                   });
  
          return View(salary);
      }
  </code></pre>