title: How to dynamically create checkboxes OR multi select on MVC 4
body: <p>In this project we have two list, one for the dealer and the second for his products.</p>
  
  <p>So far if you check one dealer we get back all the product for this specific dealer, it implemented in javascript (Json).</p>
  
  <p>Html (5 :)</p>
  
  <pre><code>@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;DealerProduct&lt;/legend&gt;
  
       &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.DealerID)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.DropDownList("DealerID", String.Empty)
          @Html.ValidationMessageFor(model =&gt; model.DealerID)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.ProductID)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
  
          @Html.DropDownList("ProductID", String.Empty)
          @Html.ValidationMessageFor(model =&gt; model.ProductID)
      &lt;/div&gt;
      &lt;p&gt;
          &lt;input type="submit" value="@Shared.Add" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>JavaScript (Json :)</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function() 
   {
    $("#DealerID").change(function ()
    {
         var self = $(this);
         var items="";
         var url = "";
         url = "@Url.Action("GetDealerProducts","DealerProduct")/"+self.val();
         $.ajaxSetup({ cache: false });
          $.getJSON(url,function(data)
          {   
            $.each(data,function(index,item)
            {
              items+="&lt;option value='"+item.Value+"'&gt;"+item.Text+"&lt;/option&gt;\n";
            });
            $("#ProductID").html(items);
             $.ajaxSetup({ cache: true }); 
          });
      });       
   });
  &lt;/script&gt;
  </code></pre>
  
  <p>Controller :</p>
  
  <pre><code>public ActionResult GetDealerProducts(int id)
      {
          Dealer currentDealer = db.Dealers.Single(p =&gt; p.UserName == User.Identity.Name);
          Dealer subDealer = db.Dealers.Single(s =&gt; s.DealerID == id);
  
  
          List&lt;Product&gt; productOpenToSale = new List&lt;Product&gt;();
  
          foreach (var item in currentDealer.ProductToSale)
              if (!subDealer.ProductToSale.ToList().Exists(e =&gt; e.ProductID == item.ProductID))
                  productOpenToSale.Add(item.Product);
  
          List&lt;SelectListItem&gt; productOpenToSaleList = new List&lt;SelectListItem&gt;();
          productOpenToSale.ForEach(item =&gt; productOpenToSaleList.Add(new SelectListItem { Value = item.ProductID.ToString(), Text = item.ProductName }));
  
          return Json(productOpenToSaleList, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p>What I really need is adding (a pairing of) products dealer, which he can sell in the future.</p>
  
  <p>Current option is to add products one by one, the desire is to give the possibility of multiple selection of all products.</p>
  
  <p>Maybe something like dynamic checkBoxList or an foreach on a List from the ViewModel who add input - checkbox <a href="https://stackoverflow.com/a/201423">like this</a>, but I don't know how to fill it after the dealer has selected on the first list and receive all the selected product back on submit..</p>
  
  <p>10X for any help!! (&amp;&amp; sorry for my bad English :)</p>