title: MVC - how to get ListBox selected item's value in Controller?
body: <p>I have an app which has two listboxes:</p>
  
  <pre><code> @using (Html.BeginForm())
          {
             @Html.ListBoxFor(m =&gt; m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} ) 
  
              &lt;button type="button" id="add"&gt;MoveRight&lt;/button&gt;
  
              &lt;button type="button" id="remove"&gt;"MoveLeft"&gt;&lt;/button&gt;
  
              &lt;button type="button" id="remove-all"&gt;RemAll&lt;/button&gt;
  
              &lt;button type="button" id="select-all"&gt;SelAll&lt;/button&gt;
  
              &lt;button type="button" id="up" onclick="MoveUp()"&gt;Up&lt;/button&gt;
  
              &lt;button type="button" id="down" onclick="MoveDown()"&gt;Down&lt;/button&gt;
  
              @Html.ListBoxFor(m =&gt; m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})
  
              &lt;br /&gt;
              &lt;p&gt;Item Caption Text (140 Characters. HTML not allowed.)&lt;/p&gt;
              @Html.TextAreaFor(m =&gt; m.ItemCaptionText)
  
              &lt;input type="submit" id="submit"  value="Save"/&gt;
          } 
  </code></pre>
  
  <p>I transfer the selected values from one listbox to the other using jQuery but I append an "L" to the value for later use:</p>
  
  <pre><code>$("#add").click(function () {
                  $("#listBoxAvail &gt; option:selected").each(function () {
                      $(this).remove();
                      val = $(this).val();
                      temp = $(this);
                      temp.val('L' + val)
                      $(temp).appendTo("#listBoxSel");
                  });
              });
  </code></pre>
  
  <p>I see the Text and Value after I transfer to the right hand listbox with the F12 Developer Tools. What I would to know is how to <strong>get that value</strong> from C# code in the Controller?</p>
  
  <pre><code>[HttpPost]
          public ActionResult Index(OptInViewModel viewModel)
          {
              AttributeEntities db = new AttributeEntities();
              IEnumerable&lt;string&gt; items = viewModel.SelectedAttributes2;
              foreach (var item in items)
              {
                  var temp = item;
  
                  // Save it
                  SelectedHarmonyAttribute attribute = new SelectedHarmonyAttribute();
                  attribute.CustomLabel = viewModel.ItemCaptionText;
                  //attribute.IsVisible = ?
                  //attribute.OrderNumber = ?
                  //attribute.HarmonyAttribute_ID
  
              }
  
              return View("Index");
          }
  </code></pre>
  
  <p>The ViewModel is:</p>
  
  <pre><code>public class OptInViewModel
      {
          public IEnumerable&lt;string&gt; SelectedAttributes { get; set; }
          public IEnumerable&lt;string&gt; SelectedAttributes2 { get; set; }
          public IEnumerable&lt;SelectListItem&gt; Attributes { get; set; }
          public IEnumerable&lt;SelectListItem&gt; SelectedItems { get; set; }
          public string ItemCaptionText { get; set; }
      }
  </code></pre>