title: Error when using a DropDownListFor in a foreach loop in an MVC Razor view
body: <p>I am trying to create a simple form in ASP.MVC which shows a list of quotes, and allows the user to create work orders by selecting a person from a drop down list to assign that quote to, and then submitting the form all in one go.</p>
  
  <p>My problem is that my use of DropDownListFor within a foreach loop is causing an error and I don't think I'm going about this the right way.</p>
  
  <p>A (somewhat simplified) quote looks like this:</p>
  
  <pre><code>public class QuoteItem
  {
      public QuoteItem()
      {
          this.IsWorkOrder = false;
      }
  
      [Key]
      public int QuoteItemCostID { get; set; }
      public string Item { get; set; }
      public decimal? Subtotal { get; set; }
      public bool IsWorkOrder { get; set; }
  
      [NotMapped]
      public string AssignedTo { get; set; }
  }
  </code></pre>
  
  <p>I have a View Model configured as follows:</p>
  
  <pre><code>public class UnassignedWorkOrdersViewModel
  {
      public IEnumerable&lt;QuoteItemCost&gt; QuoteItemCosts { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Trades { get; set; }
  }
  </code></pre>
  
  <p>And a controller action that looks like this:</p>
  
  <pre><code>public ActionResult Unassigned(int id = 0)
  {
  
      var trades = db.UserProfiles.Where(u =&gt; u.Typpe == "Trade").ToArray().Select(x =&gt; new SelectListItem
      {
          Value = x.FirstName + " " + x.LastName,
          Text = x.FirstName + " " + x.LastName
      }).ToList();
  
  
      var quoteItems = db.QuoteItems
          .Where(x =&gt; x.IsWorkOrder == false)
          .ToList();
  
      UnassignedWorkOrdersViewModel viewModel = new UnassignedWorkOrdersViewModel
      {
          Trades = trades,
          QuoteItemCosts = quoteItemCosts
      };
  
      return View(viewModel);
  }
  </code></pre>
  
  <p>My problem is that in my view, I'm trying to show each work order and the drop down list of trades next to it, and the way that I thought I'd do it is to map the class property "AssignedTo" to the value in the drop down list, however this doesn't work, as the DropDownList expects a LINQ expression as the first parameter:</p>
  
  <pre><code>@model Project.ViewModels.UnassignedWorkOrdersViewModel
  
  @if (Model.QuoteItemCosts.Any())
  {
      using (Html.BeginForm("Unassigned", "WorkOrder", FormMethod.Post, null))
      {
      @Html.AntiForgeryToken()
      &lt;table&gt;
  
          @foreach (var item in Model.QuoteItemCosts)
          {
  
              &lt;tr&gt;
                  &lt;td style="width:100px;"&gt;
                      &lt;b&gt;Item:&lt;/b&gt; @item.Item
                  &lt;/td&gt;
                  &lt;td style="width:200px;"&gt;
                      &lt;b&gt;Total:&lt;/b&gt; @item.Subtotal
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;b&gt;Assign:&lt;/b&gt;
                      @Html.DropDownListFor(
                          item.AssignedTo,  // THIS LINE CAUSES AN ERROR 
                          Model.Trades,
                          "", new { @id = "ddlStatus", @style = "width:100%" })
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/table&gt;
  }
  </code></pre>
  
  <p>How can I resolve this and am I even going about this the right way?  On form submit, I should expect to get back the ViewModel which would then return all my QuoteItems, but with an AssignedTo entry if the user has selected a person from the drop down list.</p>
  
  <p>Edit:</p>
  
  <p>Andrei's suggestion below has helped and led me to another solution where my DropDownListFor now looks like this:</p>
  
  <pre><code> @Html.DropDownListFor(
                          m =&gt; m.QuoteItemCosts.First(q =&gt; q.QuoteItemCostID == item.QuoteItemCostID).AssignedTo,
      Model.Trades,
      "", new { @id = "ddlStatus", @style = "width:100%" })
  </code></pre>
  
  <p>but when I submit the form the viewmodel is null:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Unassigned(UnassignedWorkOrdersViewModel viewModel)
      {
          // My viewModel is null
          return RedirectToAction("Index");
      }
  </code></pre>
  
  <p>I've updated my razor code to include a Html.BeginForm as well.</p>