title: ASP.NET MVC3: Object from DropdownList in Null in Controller
body: <p>I have created a sample MVC3 application for BookShop. I have a create action. In the create action users enter name of a book, author name and select a genre name. It is working without errors. But in the controller, I am not getting the selected genre name ( - the genre object comes as null). How do we correct it?</p>
  
  <p><strong>Note:</strong> When I write <code>@Html.DropDownList("GenreId", String.Empty)</code> the Model Binding capabilities built into ASP.NET MVC, will attempt to populate an object of that type using form inputs. E.g. it will try to set the book object’s GenreId value.  But Book does not contain the property GenreId. However the dropdown can show the values by reading required values from ViewBag.</p>
  
  <p>CODE</p>
  
  <pre><code>@model MyBookShopApp.Book
  
  @{
  ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
  
  @Html.ValidationSummary(true)
  
  &lt;fieldset&gt;
  
      &lt;legend&gt;BOOK&lt;/legend&gt;
  
  
      &lt;div &gt;
          @Html.LabelFor(model =&gt; model.Title) :~: @Html.EditorFor(model =&gt; model.Title)
      &lt;/div&gt;
  
  
      &lt;div &gt;
          @Html.LabelFor(model =&gt; model.Artist.Name )  :*: @Html.EditorFor(model =&gt; model.Artist.Name)
      &lt;/div&gt;
  
  
      &lt;div &gt;
          @Html.LabelFor(model =&gt; model.GenreEntity.Name, "The Genre") ::   @Html.DropDownList("GenreId", String.Empty)
      &lt;/div&gt;
  
  
      &lt;p&gt;
          &lt;input type="submit" value="Create" /&gt;
      &lt;/p&gt;
  
  &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
   @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>CONTROLLER</p>
  
  <pre><code>namespace MyBookShopApp.Controllers
  {
  public class StoreManagerController : Controller
  {
  
      List&lt;GenreEntity&gt; listOfGenres = new List&lt;GenreEntity&gt;()
                                      {
                                         new GenreEntity { Name = "Fiction", GenreId = 1 },
                                         new GenreEntity { Name = "Science", GenreId = 2 },
                                         new GenreEntity { Name = "Religious", GenreId = 3 },
                                      };
  
      List&lt;Book&gt; bookList = new List&lt;Book&gt;()
                            {
                              new Book
                              {
                                  BookId = 1,Title = "Book1",
                                  GenreEntity = new GenreEntity { Name = "Fiction", GenreId = 1 },
                                  Artist = new Artist { ArtistId = 1, Name = "Dinesh" }
                              },
                              new Book
                              {
                                  BookId = 2,Title = "Book2",
                                  GenreEntity = new GenreEntity { Name = "Science", GenreId = 2 },
                                  Artist = new Artist { ArtistId = 1, Name = "Lijo" }
                              },
                              new Book
                              {
                                  BookId = 3,Title = "Book3",
                                  GenreEntity = new GenreEntity { Name = "Religious", GenreId = 3 },
                                  Artist = new Artist { ArtistId = 1, Name = "Santhosh" }
                              }
  
                            };
  
  
  
  
  
      #region CREATE
  
  
  
      // GET: 
      public ActionResult Create()
      {
                      ViewBag.GenreId = new SelectList(listOfGenres, "GenreId", "Name");
          return View();
      }
  
      // POST:
      [HttpPost]
      public ActionResult Create(Book theBook)
      {
          if (ModelState.IsValid)
          {
              //Save the book in DB first and then redirectToAction.
              return RedirectToAction("Index");
          }
  
          return View(theBook);
      }
  
      #endregion
  
  
  }
  }
  </code></pre>
  
  <p>Book Class</p>
  
  <pre><code>public class Book
  {
      public int BookId { get; set; } 
      public string Title { get; set; }
      public GenreEntity GenreEntity { get; set; }
      public Artist Artist { get; set; }
  
  }
  </code></pre>
  
  <p>GenreEntity</p>
  
  <pre><code>  public class GenreEntity
  {
      public string Name { get; set; }
      public int GenreId { get; set; }
  }
  </code></pre>
  
  <p>Artist Class</p>
  
  <pre><code> public class Artist
  {
      public int ArtistId { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p><strong>REFERENCE</strong>:</p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/4823904/dropdown-in-mvc3-edit-form">dropdown in mvc3 edit form</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/4904946/how-to-use-a-html-dropdownlist-in-a-strongly-typed-view-for-a-model-containing-a">How to use a Html.DropDownList in a strongly-typed view for a model containing a nullable enum property?</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/7714490/mvc3-html-helper-doesnt-update-dropdownlistfor-on-submit-the-form">MVC3 HTML helper doesn&#39;t update DropdownListFor on Submit the form</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/5783344/on-select-change-event-html-dropdownlistfor">on select change event - Html.DropDownListFor</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/8957000/mvc3-html-dropdownlistfor-not-populating-selected-item">MVC3 @Html.DropDownListFor not populating selected item</a></p></li>
  </ol>