title: Passing Model data from View to Controller
body: <p>I am trying to pass the Model data from a View (and PartialView within the View) back to the Controller upon HttpPost. (Adapted from <a href="https://stackoverflow.com/questions/10187469/pass-selectedvalue-of-dropdownlist-in-html-beginform-in-asp-net-mvc-3">Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3</a>)</p>
  
  <p>Why? I want to show a list of assets each with a DropDownList and number of options. Upon submission of form to read the selected items from DropDownList.</p>
  
  <p>My 2 (simplified) models:</p>
  
  <pre><code>public class Booking
  {
      public int BookingID { get; set; }
      public int StoreID { get; set; }
      ...
      public IEnumerable&lt;AssetShort&gt; Assets { get; set; }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class AssetShort
  {
      public int AssetID { get; set; }
      ....
      public int SelectedAction { get; set; }
      public IEnumerable&lt;SelectListItem&gt; ActionList { get; set; }
  }
  </code></pre>
  
  <p>In my Booking Controller > Create I build the List:</p>
  
  <pre><code>public ActionResult Booking(int id)
      {
       // get myBag which contains a List&lt;Asset&gt;
       //  booking corresponds to 'id'
  
          var myAssets = new List&lt;AssetShort&gt;();
          foreach (var a in myBag.Assets)
          {
              var b = new AssetShort();
              b.AssetID = a.ID;
              b.SelectedAction = 0;
              b.ActionList = new[]
                  { 
              new SelectListItem { Selected = true, Value = "0", Text = "Select..."},
              new SelectListItem { Selected = false, Value = "1", Text = "Add"},
              new SelectListItem { Selected = false, Value = "2", Text = "Remove"},
              new SelectListItem { Selected = false, Value = "3", Text = "Relocate"},
              new SelectListItem { Selected = false, Value = "4", Text = "Upgrade"},
              new SelectListItem { Selected = false, Value = "5", Text = "Downgrade"}
                  };
              myAssets.Add(b);
          };
  
          var model = new BookingRequirementsViewModel
              {
                  BookingID = booking.ID,
                  StoreID = booking.StoreID,
                  Assets = myAssets.ToList(),
              };
          return View(model);
  </code></pre>
  
  <p>My View:</p>
  
  <pre><code>@model uatlab.ViewModels.BookingRequirementsViewModel
  @{
      ViewBag.Title = "Booking step 2";
  }
  &lt;h4&gt;Your booking ref. @Model.BookingID&lt;/h4&gt;
  @using (Html.BeginForm("Booking2", "Booking", FormMethod.Post))
  {
  &lt;fieldset&gt;
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model =&gt; model.StoreID)
  
    @Html.Partial("_Assets", Model.StoreAssets)
  
    &lt;input type="submit" value="Cancel" class="btn btn-default" /&gt;
    &lt;input type="submit" value="Next" class="btn btn-default" /&gt;
  &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>The Partial View includes</p>
  
  <pre><code>@foreach (var item in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;@item.Name&lt;/td&gt;
          &lt;td&gt;@item.Number&lt;/td&gt;
          &lt;td&gt;@Html.DropDownListFor(modelItem=&gt;item.SelectedAction, item.ActionList)&lt;/td&gt;
      &lt;/tr&gt;
  }
  </code></pre>
  
  <p>So, all this works fine in the browser and I can select dropdowns for each asset listed but when I submit the only value posted back is the StoreID as it is in a "HiddenFor".</p>
  
  <p>The booking2 controller has the model for a parameter:</p>
  
  <pre><code>public ActionResult Booking2(BookingRequirementsViewModel model)
  {
      //loop through model.Assets and display SelectedActions
  }
  </code></pre>
  
  <p>Let me make it clear what the problems is - in Booking2 controller the Model is null when viewed in Debug mode and I get error "Object reference not set to an instance of an object." </p>
  
  <p>Any ideas please how to pass back the Model to controller from view?</p>
  
  <p>Regards
  Craig</p>