title: How to pass selected item id from dropdown list to controller's action?
body: <p><strong>General problem overview:</strong> I have a create\edit view for a compound item, where I select lots of options for components from drop down lists. I want to be able to add or edit a specific component, selected in a dropdown list. </p>
  
  <p><strong>Here is a short, but complete example</strong>:</p>
  
  <p>Suppose I have a <code>Car</code> model</p>
  
  <pre><code>public class Car
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public BodyStyle BodyStyle { get; set; }
      public int BodyStyleId { get; set; }
  }
  </code></pre>
  
  <p><code>BodyStyle</code> is a component of a <code>Car</code> aggregate, defined as:</p>
  
  <pre><code>public class BodyStyle
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Now I have a controller action to create a car, </p>
  
  <pre><code>public ActionResult CreateCar()
  {
      //BodyStyles is a mock property with all hardcoded BodyStyles, just for demo
      ViewBag.Styles = new SelectList(BodyStyles, "Id", "Name");
      return View();
  }
  </code></pre>
  
  <p>Here is the part of the <code>CreateCar</code> view, which defines <code>BodyStyle</code> dropdown list. </p>
  
  <pre><code>&lt;div class="form-group"&gt;
      @Html.LabelFor(model =&gt; model.BodyStyle, new { @class = "control-label col-md-2" })
      &lt;div class="col-md-10"&gt;
          @Html.DropDownListFor(model =&gt; model.BodyStyleId, (IEnumerable&lt;SelectListItem&gt;)ViewBag.Styles)
          @Html.ActionLink("Create", "CreateBodyStyle")
  
          @*here is the problem, can't find a way to pass selected BodyStyle Id to EditBodyStyle action*@
          @Html.ActionLink("Edit", "EditBodyStyle") 
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is the naive implementation of <code>EditBodyStyle</code></p>
  
  <pre><code>public ActionResult EditBodyStyle(int bodyStyleId)
  {
      return View(BodyStyles.First(b =&gt; b.Id == bodyStyleId));
  }
  </code></pre>
  
  <p><strong>Is there a way to say to ASP.NET MVC to pass the selected BodyStyle Id from <code>DropDownListFor</code> to <code>EditBodyStyle</code> action?</strong></p>
  
  <p>I've achieved this with javascript, subscribing to <code>onchange</code> and storing the selected value, then using it in the link. But the code becomes a <strong>mess</strong>, since I have near 10 of such components on several views. The functionality is quite simple, so I wonder if I'm missing some ASP.NET MVC feature, that will help me achieve this only using Razor view engine. </p>
  
  <p>Here is the resulting view, just to get the whole picture:</p>
  
  <p><img src="https://i.stack.imgur.com/Ic66d.png" alt="enter image description here"></p>
  
  <p>Where each option of <code>select</code> tag is of format:</p>
  
  <pre><code>&lt;option value="1"&gt;Coupe - 2 door&lt;/option&gt;
  &lt;option value="2"&gt;Coupe - 3 door&lt;/option&gt;
  </code></pre>
  
  <p><strong>Current implementation</strong> (there is no id's in the view example above, but assume dropdown list and link has ids, as noted in the js):</p>
  
  <pre><code>$(function () {
      $("#editBodyStyleLink").click(function () {
          var id = $("#bodyStylesDropDown").val();
          window.location.href = "/Home/EditBodyStyle/?id=" + id;
      });
  });
  </code></pre>