title: C# MVC Dropdownlist not updating Model
body: <p>I am having problem understanding as to why my model is not updating when I select a new value from my dropdownlist control?</p>
  
  <p>Here is my model</p>
  
  <pre><code>public class UserViewModel
  {
      public Users users { get; set; }                
      public IEnumerable&lt;SelectListItem&gt; UserRoles { get; set; }                
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>//GET
  public ActionResult Edit(int id)
  {
          var vm = new UserViewModel();
          vm.users = repository.GetById(id);
  
          vm.UserRoles = db.UserRoles.Select(
                              x =&gt; new SelectListItem
                              {
                                  Selected = true,
                                  Text = x.UserRoleName,
                                  Value = x.UserRoleID.ToString()
                              }
              );
  
          if (vm == null)
          {
              return HttpNotFound();
          }
          return View(vm);
     }
  
  
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Edit(UserViewModel model)
  {
          if(ModelState.IsValid)
          {
              db.Entry(model).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View();       
   }
  </code></pre>
  
  <p>And finally my View</p>
  
  <pre><code>&lt;div class="form-group"&gt;
  &lt;label class="control-label col-md-2"&gt;User Role&lt;/label&gt;
  &lt;div class="col-md-10"&gt;
         @Html.HiddenFor(model =&gt; model.users.UserRoleID)
         @Html.DropDownListFor(model =&gt; model.UserRoles, (IList&lt;SelectListItem&gt;)ViewBag.UserRoles, "-- Select One --", new { @class = "form-control" })                
       &lt;/div&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>I have stepped through the code and in the Collection can see <code>UserRoles</code> in the collection but I am not sure if I am passing the value correctly? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have updated my <code>POST</code> method for updating the model</p>
  
  <pre><code>public ActionResult Edit(int id, UserViewModel model)
  {
          var user = repository.GetById(id);
          if (ModelState.IsValid)
          {
              if (user != null)
              {
                  user.Username = model.users.Username;
                  user.Forename = model.users.Forename;
                  user.Lastname = model.users.Lastname;
                  user.Email = model.users.Email;
                  user.Status = model.users.Status;
                  user.UserRoleID = Convert.ToInt32(model.UserRoles);
  
                  db.Entry(user).State = EntityState.Modified;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              else
              {
                  return View();
              }                
          }
          return View();         
      }
  </code></pre>
  
  <p>However on Submit it is giving me a <code>Null reference</code> exception on the dropdownlist as shown below? Now sure why?</p>
  
  <p><a href="https://i.stack.imgur.com/Ef1T9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ef1T9.jpg" alt="Null reference exception"></a></p>