title: ASP.NET MVC 4 Navigation Virtual Property not populated on Post Action
body: <p>I have a navigation property (Category) on a Question class for which I am manually creating a DropDownList for in the Create view of Question, and when posting the Create action, the Category navigation property is not populated on the Model, therefore giving me an invalid ModelState.</p>
  
  <p>Here is my model:</p>
  
  <pre><code> public class Category
      {
          [Key]
          [Required]
          public int CategoryId { get; set; }
  
          [Required]
          public string CategoryName { get; set; }
  
          public virtual List&lt;Question&gt; Questions { get; set; }
      }
  
  public class Question
      {
          [Required]
          public int QuestionId { get; set; }
  
          [Required]
          public string QuestionText { get; set; }
  
          [Required]
          public string Answer { get; set; }
  
          [ForeignKey("CategoryId")]
          public virtual Category Category { get; set; }
  
          public int CategoryId { get; set; }
      }
  </code></pre>
  
  <p>Here is my Question controller for both GET and POST actions of Create:</p>
  
  <pre><code>public ActionResult Create(int? id)
          {
              ViewBag.Categories = Categories.Select(option =&gt; new SelectListItem {
                  Text = option.CategoryName,
                  Value = option.CategoryId.ToString(),
                  Selected = (id == option.CategoryId)
              });
              return View();
          }
  
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create(Question question)
          {
              if (ModelState.IsValid)
              {
                  db.Questions.Add(question);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(question);
          }
  </code></pre>
  
  <p>And here is the Create view for Question</p>
  
  <pre><code>@using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;Question&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Category)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownListFor(model =&gt; model.Category.CategoryId, (IEnumerable&lt;SelectListItem&gt;)ViewBag.Categories, "Select a Category")
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.QuestionText)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.QuestionText)
              @Html.ValidationMessageFor(model =&gt; model.QuestionText)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Answer)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Answer)
              @Html.ValidationMessageFor(model =&gt; model.Answer)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>I have tried the following variations of generating the dropdownlist on the view:</p>
  
  <pre><code>@Html.DropDownListFor(model =&gt; model.Category.CategoryId, (IEnumerable&lt;SelectListItem&gt;)ViewBag.Categories, "Select a Category")
  @Html.DropDownListFor(model =&gt; model.Category, (IEnumerable&lt;SelectListItem&gt;)ViewBag.Categories, "Select a Category")
  @Html.DropDownList("Category", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Categories, "Select a Category")
  @Html.DropDownList("CategoryId", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Categories, "Select a Category")
  </code></pre>
  
  <p>When I quickwatch the Question object on the POST action, the Category property is null, but the CategoryId field on the property is set to the selected Category on the view.</p>
  
  <p>I know I could easily add code to manually fetch the Category with EF by using the CategoryId value that I get from the view. I also think I could create a custom binder to do this, but I was hoping that this could be done with data annotations.</p>
  
  <p>Am I missing something? </p>
  
  <p>Is there a better way to generate the dropdownlist for the navigation property?</p>
  
  <p>Is there a way to let MVC know how to populate the navigation property without me having to manually do it?</p>
  
  <p>-- EDIT:</p>
  
  <p>If it makes any difference, I do not need the actual navigation property loaded when creating/saving the Question, I just need the CategoryId to be correctly saved to the Database, which isn't happening.</p>
  
  <p>Thanks</p>