title: Validation DropDownList value from ViewModel in ASP.Net MVC
body: <p>I tried to search posts, without any result either, maybe I didn't use the right words.</p>
  <p>I need a solution in <code>MVC</code> for validate the <code>DropDownList</code> value, populated from database using <code>Model class</code> and the <code>Html.DropDownListFor Helper</code> method and MySql.</p>
  <p>In the view I have added new <code>DDL</code> and this is populated correctly from database</p>
  <pre><code>    @Html.DropDownListFor(m =&gt; m.Fruits, Model.Fruits, &quot;[ === Please select === ]&quot;, new { @Class = &quot;textarea&quot; })
      @Html.ValidationMessageFor(m =&gt; m.Fruits, &quot;&quot;, new { @class = &quot;text-danger&quot; })
  
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;div class=&quot;col-md-offset-5 col-md-10&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-default&quot; /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  <p>But when on click the</p>
  <pre><code>&lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-default&quot; /&gt;
  </code></pre>
  <p>The form is validate and does not stop sending when the <code>DDL</code> has not selected any value.</p>
  <p>Without <code>DDL</code> the validation working correctly.</p>
  <p>Please, help me.</p>
  <p>My code below</p>
  <p><strong>Model</strong></p>
  <pre><code>namespace InsGE.Models
  {
      public class PersonModel
      {
          [Required]
          [Display(Name = &quot;Fruits&quot;)] 
          public List&lt;SelectListItem&gt; Fruits { get; set; }
  
          public string Namex { get; set; }
          
          public string Codex { get; set; }
  
          [Required]
          [Display(Name = &quot;CL&quot;)] 
          public string CL { get; set; }
  
          [Required]
          [Display(Name = &quot;Ticket&quot;)]
          public string Ticket { get; set; }
      }
  }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code>namespace InGE.Controllers
  {
      public class HomeController : Controller
      {
          private static List&lt;SelectListItem&gt; PopulateFruits()
          {
              string sql;
  
              List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;();
  
              string constr = ConfigurationManager.ConnectionStrings[&quot;cn&quot;].ConnectionString;
  
              using (MySqlConnection con = new MySqlConnection(constr))
              {
                  sql = @String.Format(&quot;SELECT * FROM `dotable`; &quot;);
  
                  using (MySqlCommand cmd = new MySqlCommand(sql))
                  {
                      cmd.Connection = con;
                      con.Open();
  
                      using (MySqlDataReader sdr = cmd.ExecuteReader())
                      {
                          while (sdr.Read())
                          {
                              items.Add(new SelectListItem
                              {
                                  Text = sdr[&quot;sName&quot;].ToString(),
                                  Value = sdr[&quot;sCode&quot;].ToString()
                              });
                          }
                      }
                      con.Close();
                  }
              }
  
              return items;
          }
  
          [HttpPost]
          public ActionResult Index(PersonModel person)
          {
              string sCl = person.CL;
              string sTicket = person.Ticket;       
              string sName = person.Namex;
              string sCode = person.Codex;
  
              return View();
          }
  
          [HttpGet]
          public ActionResult Index()
          {
              PersonModel fruit = new PersonModel();
              fruit.Fruits = PopulateFruits();
              return View(fruit);
          }
  
          public ActionResult About()
          {
             ViewBag.Message = &quot;Your application description page.&quot;;    
             return View();
          }
  
          public ActionResult Contact()
          {
             ViewBag.Message = &quot;Your contact page.&quot;;    
             return View();
          }
  </code></pre>
  <p><strong>update</strong></p>
  <p><strong>Controller</strong></p>
  <pre><code>   public class HomeController : Controller
      {
          public class Fruit
          {
              public string Code { get; }
              public string Name { get; }
  
              public Fruit(string code, string name)
              {
                  Code = code;
                  Name = name;
              }
          }
  
          public class FruitsRepository
          {
              private static List&lt;Fruit&gt; GetAll()
              {
                  string sql;
  
                  var fruits = new List&lt;Fruit&gt;();
  
                  string constr = ConfigurationManager.ConnectionStrings[&quot;cn&quot;].ConnectionString;
  
                  using (MySqlConnection con = new MySqlConnection(constr))
                  {
                      sql = @String.Format(&quot;SELECT * FROM `dotable`; &quot;);
  
                      using (MySqlCommand cmd = new MySqlCommand(sql))
                      {
                          cmd.Connection = con;
                          con.Open();
  
                          using (MySqlDataReader sdr = cmd.ExecuteReader())
                          {
                              while (sdr.Read())
                              {
                                  var fruit = new Fruit(sdr[&quot;sCode&quot;].ToString(), sdr[&quot;sName&quot;].ToString());
                                  fruits.Add(fruit);
                              }
                          }
                          con.Close();
                      }
                  }
  
                  return fruits;
              }
          }
  
          [HttpGet]
          public ActionResult Index() &lt;&lt;&lt;&lt;&lt;&lt; Error “not all code paths return a value”
          {
              var personModel = new PersonModel();
              var fruitsRepo = new FruitsRepository();
              var fruits = fruitsRepo.GetAll(); &lt;&lt;&lt;&lt;&lt;&lt; Error “is inaccessible due to its protection level”
              var fruitsSelecteListItems = fruits.Select(fruit =&gt; new SelectListItem
              {
                  Text = fruit.Name;
                  Value = fruit.Code; &lt;&lt;&lt;&lt;&lt;&lt; Error “The name 'Value' does not exist in the current context”
              });
          return View(personModel); &lt;&lt;&lt;&lt;&lt;&lt;Error “The type or namespace name could not be found”
      }
  </code></pre>
  <p><strong>Model</strong></p>
  <pre><code>public class PersonModel
  {
      [Required]
      [Display(Name = &quot;Fruits&quot;)] 
      public string SelectedFruitCode { get; set; }
  
      public List&lt;SelectListItem&gt; Fruits { get; set; }
  
      public string Namex { get; set; }
      
      public string Codex { get; set; }
  
      [Required]
      [Display(Name = &quot;CL&quot;)] 
      public string CL { get; set; }
  
      [Required]
      [Display(Name = &quot;Ticket&quot;)]
      public string Ticket { get; set; }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/n0cZ5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n0cZ5.png" alt="enter image description here" /></a></p>
  <p><strong>Complete View</strong></p>
  <pre><code>@using (Html.BeginForm(&quot;Index&quot;, &quot;Home&quot;, FormMethod.Post))
  {
      @Html.AntiForgeryToken()
      &lt;div&gt;
          @Html.ValidationSummary(true, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td class=&quot;textarea&quot;&gt;CL&lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBoxFor(m =&gt; m.CL, new { @Class = &quot;textarea&quot;, placeholder = &quot;CL&quot; })
                      @Html.ValidationMessageFor(m =&gt; m.CL, &quot;&quot;, new { @class = &quot;text-danger&quot; })
                  &lt;/td&gt;
                  &lt;td class=&quot;textarea&quot;&gt;&lt;/td&gt;
              &lt;tr&gt;
                  &lt;td class=&quot;textarea&quot;&gt;Ticket&lt;/td&gt;
                  &lt;td&gt;
                      @Html.TextBoxFor(m =&gt; m.Ticket, new { @Class = &quot;textarea&quot;, placeholder = &quot;Ticket&quot; })
                      @Html.ValidationMessageFor(m =&gt; m.Ticket, &quot;&quot;, new { @class = &quot;text-danger&quot; })
                  &lt;/td&gt;
                  &lt;td class=&quot;textarea&quot;&gt;&lt;/td&gt;
                  &lt;td class=&quot;textarea&quot;&gt;Fruits&lt;/td&gt;
                  &lt;td&gt;
                      @Html.DropDownListFor(m =&gt; m.SelectedFruitCode, Model.Fruits, &quot;[ === Please select === ]&quot;, new { @Class = &quot;textarea&quot; })
                      @Html.ValidationMessageFor(m =&gt; m.SelectedFruitCode, &quot;&quot;, new { @class = &quot;text-danger&quot; })
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;br /&gt;
          &lt;hr class=&quot;new3&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
              &lt;div class=&quot;col-md-offset-5 col-md-10&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-default&quot; /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  &lt;div&gt;
      @Html.ActionLink(&quot;Back to List&quot;, &quot;Index&quot;)
  &lt;/div&gt;
  
  @section Scripts {
  
      @Scripts.Render(&quot;~/bundles/jqueryui&quot;)
      @Styles.Render(&quot;~/Content/cssjqryUi&quot;)
  
      &lt;script type=&quot;text/javascript&quot;&gt;
          $(document).ready(function () {
              $('input[type=datetime]').datepicker({
                  dateFormat: &quot;dd/mm/yy&quot;,
                  changeMonth: true,
                  changeYear: true,
                  yearRange: &quot;-2:+0&quot;
              });
          });
      &lt;/script&gt;
  }
  </code></pre>
  <p><strong>Complete Controller</strong></p>
  <pre><code>public class HomeController : Controller
  {
  
      [HttpPost]
      public ActionResult Index(PersonModel person)
      {
          if (ModelState.IsValid)
          {
              string cl = person.CL;
              string ticket = person.Ticket;
          }
  
          return View();
      }
  
     public class Fruit
      {
          public string Code { get; }
          public string Name { get; }
  
          public Fruit(string code, string name)
          {
              Code = code;
              Name = name;
          }
      }
  
      public class FruitsRepository
      {
          public List&lt;Fruit&gt; GetAll()
          {
              string sql;
  
              var fruits = new List&lt;Fruit&gt;();
  
              string constr = ConfigurationManager.ConnectionStrings[&quot;cnx&quot;].ConnectionString;
  
              using (MySqlConnection con = new MySqlConnection(constr))
              {
                  sql = @String.Format(&quot;SELECT * FROM `dotable`; &quot;)
  
                  using (MySqlCommand cmd = new MySqlCommand(sql))
                  {
                      cmd.Connection = con;
                      con.Open();
  
                      using (MySqlDataReader sdr = cmd.ExecuteReader())
                      {
                          while (sdr.Read())
                          {
                              var fruit = new Fruit(sdr[&quot;sCode&quot;].ToString(), sdr[&quot;sName&quot;].ToString());
                              fruits.Add(fruit);
                          }
                      }
                      con.Close();
                  }
              }
  
              return fruits;
          }
      }
  
      [HttpGet]
      public ActionResult Index()
      {
          var personModel = new PersonModel();
          var fruitsRepo = new FruitsRepository();
          var fruits = fruitsRepo.GetAll();
          var fruitsSelecteListItems = fruits.Select(fruit =&gt; new SelectListItem
          {
              Text = fruit.Name,
              Value = fruit.Code
          }).ToList();
          personModel.Fruits = fruitsSelecteListItems;
          return View(personModel);
      }
  
      public ActionResult About()
      {
          ViewBag.Message = &quot;Your application description page.&quot;;
          return View();
      }
  
      public ActionResult Contact()
      {
          ViewBag.Message = &quot;Your contact page.&quot;;
          return View();
      }
  }
  </code></pre>