title: Asp.net core 6 get value from drop downlist
body: <p>I am using the @Html.dropdownlistfor that allows the user to choose the customer that creates the deal. But the chhosed customer did not proceed to the controller which led to an unvalid model state. I got the reason that the model was not valid</p>
  <p><a href="https://i.stack.imgur.com/uCwRD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uCwRD.png" alt="Error" /></a></p>
  <p>but I did not know how to solve it. I have followed these links but the problem is still the same.</p>
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/65312026/how-do-i-get-the-selected-value-of-a-dropdownlist-in-asp-net-core-mvc-app">How do I get the Selected Value of a DropDownList in ASP.NET Core
  MVC App</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/27901175/how-to-get-dropdownlist-selectedvalue-in-controller-in-mvc">How to get DropDownList SelectedValue in Controller in MVC</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/64949615/selectlist-from-viewmodel-from-repository-asp-net-core">SelectList from ViewModel from repository Asp.net core</a></p>
  </li>
  </ol>
  <p>Sorry if my question is naive I am new to ASP.net core MVC.</p>
  <p>the code is as below:</p>
  <pre><code>    namespace MyShop.ViewModels
  {
      public class CreateDealVM
      {
          public Deals deal { get; set; }
          public IEnumerable&lt;SelectListItem&gt; CustomerListVM { get; set; }
  
      }
  }
  </code></pre>
  <p><strong>The Model class:</strong></p>
  <pre><code> namespace MyShop.Models
  {
      [Table(&quot;Deals&quot;)]
      public class Deals
      {
          [Key]
          [Display(Name = &quot;ID&quot;)]
          public int dealId { get; set; }
  
          [ForeignKey(&quot;Customer&quot;)]
          [Display(Name = &quot;Customer&quot;)]
          public Customer customer { get; set; }
  
          [Display(Name = &quot;CustomerName&quot;)]
          public string? parentCustomerName { get; set; }
  
          [Display(Name = &quot;product&quot;)]
          public DealTypeEnum product { get; set; }
  
          [Display(Name = &quot;Date&quot;)]
          public DateTime saleDate { get; set; }
  
          [Display(Name = &quot;Quantity&quot;)]
          public float quantity { get; set; }
  
          [Display(Name = &quot;Price&quot;)]
          public float price { get; set; }
      }
  </code></pre>
  <p><strong>The Controller:</strong></p>
  <pre><code>  public IActionResult Create()
          {
              CreateDealVM vm = new CreateDealVM();
              vm.CustomerListVM  = _context.Customers.Select(x =&gt; new SelectListItem { Value = x.customerId.ToString(), Text = x.customerName }).ToList();
              return View(vm);
          }
  
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(&quot;dealId,customer,product,saleDate,quantity,price&quot;)] Deals deal, CreateDealVM vm)
          {
      
              if (ModelState.IsValid)
              {
                  try
                  {
                      vm.deal.customer = _context.Customers.Find(vm.CustomerListVM);
  
                      vm.deal = deal;
                      _context.Deals.Add(vm.deal);
  
                      _context.SaveChanges();
  
                      return RedirectToAction(&quot;Index&quot;);
  
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine(e.Message);
                  }
  
  
                  return View(vm);
              }
              else
              {
  
                  var errors = ModelState.Select(x =&gt; x.Value.Errors)
                      .Where(y =&gt; y.Count &gt; 0)
                      .ToList();
                      
              //The Error showed here. 
               }
              return View(vm);
          }
  </code></pre>
  <p><strong>The view:</strong></p>
  <pre><code>@model MyShop.ViewModels.CreateDealVM
  @{
      ViewData[&quot;Title&quot;] = &quot;Create&quot;;
  }
  &lt;h1&gt;Create&lt;/h1&gt;
  &lt;h4&gt;Deals&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=&quot;Create&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
               &lt;div class=&quot;form-group&quot;&gt;
                  @Html.LabelFor(model =&gt; model.deal.customer, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
                  &lt;div class=&quot;col-md-10&quot;&gt;
                      @Html.DropDownListFor(model =&gt; model.CustomerListVM,Model.CustomerListVM,&quot;Select Customer&quot;, htmlAttributes: new { @class = &quot;form-control&quot;})
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;deal.product&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;select asp-for=&quot;deal.product&quot; class=&quot;form-control&quot;&gt;&lt;/select&gt;
                  &lt;span asp-validation-for=&quot;deal.product&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;deal.saleDate&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;deal.saleDate&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;deal.saleDate&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;deal.quantity&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;deal.quantity&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;deal.quantity&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;deal.price&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;deal.price&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;deal.price&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>