title: ASP.NET MVC : add dynamically cascading dropdownlist
body: <p>I have 2 dropdownlist : Teams and Players
  When I choose a Team, players for these Team are displayed in dropdownlist "Players".
  I do it thanks to javascript (who get Team Id in the dropdownlist) and partial view for the second dropdownlist. It work very well.</p>
  
  <p>Next, I want to dynamically add a new line of this cascading dropdownlist.
  Currently, I'm abble to do it but my problem is that in the second line, the dropdownlist "Players" (DL 4) get value of the first dropdownlist "Team" (DL1) and not from the dropdownlist of his line (DL3).
  And it's the same if I add another lines : in dropdownlist "Team" I've all teams but in the dropdownlist "Players" I've values from the selected Id in the first dropdownlist "Team".</p>
  
  <p><a href="https://i.stack.imgur.com/fBnUr.png" rel="nofollow noreferrer">workflow</a></p>
  
  <p>======================================================</p>
  
  <p>EDITION : </p>
  
  <p>2 images added to clarify my problem  => <a href="https://i.stack.imgur.com/kYz6z.png" rel="nofollow noreferrer">Database</a> and <a href="https://i.stack.imgur.com/lPVQg.png" rel="nofollow noreferrer">Workflow</a></p>
  
  <p>As you can see in the image "Workflow", the list of players displayed in panel 9 for the new line is wrong. The list of players refers to the first team selected and not (as it should be) to the second selected Team.</p>
  
  <p>======================================================</p>
  
  <p><strong><em>My Code :</em></strong></p>
  
  <p><em>In View :</em></p>
  
  <pre><code>                        &lt;form method="post"&gt;
                  &lt;a style="margin: 10px 0;" href="javascript:addRow();"&gt;Add Row&lt;/a&gt;
                  &lt;table id="formTable"&gt;
                      &lt;thead&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Team&lt;/th&gt;
                          &lt;th&gt;Player&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;/thead&gt;
                      &lt;tbody&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  @if (ViewBag.TeamList != null)
                                  {
                                      @Html.DropDownListFor(m =&gt; m.TeamId, ViewBag.TeamList as SelectList, "-- Select Team--", new { @class = "form-control" })
  
                                  }
                              &lt;/td&gt;
                              &lt;td&gt;
                                  @Html.DropDownListFor(m =&gt; m.PlayerId, new SelectList(""), "--Select Player--", new { @class = "form-control" })
                              &lt;/td&gt;
                          &lt;/tr&gt;                           
                      &lt;/tbody&gt;
                  &lt;/table&gt;
              &lt;/form&gt;
  
  
  
  
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  
      @Styles.Render("~/Content/css")
  
      @Styles.Render("~/Script/js")
  
  &lt;!-- Cascading dropdownlist--&gt;
  &lt;script&gt;
  $(document).ready(function () {
      $("#TeamId").change(function () {
          var teamId = $(this).val();
          debugger
          $.ajax({
              type: "Post",
              url: "/OCs/GetPlayerList?TeamId=" + teamId,
              contentType:"html",
              success: function (response) {
                  debugger
                  $("#PlayerId").empty();
                  $("#PlayerId").append(response);
              }
          })
      })
  })
  &lt;/script&gt;
  
  &lt;!--  Add new row--&gt;
  &lt;script type="text/javascript"&gt;// &lt;![CDATA[
      function addRow() {
  
          //copy the table row and clear the value of the input, then append the row to the end of the table
          $("#formTable tbody tr:first").clone().find("select").each(function () {
              $(this).val('');
          }).end().appendTo("#formTable");
      }
  // ]]&gt;&lt;/script&gt;
  </code></pre>
  
  <p><em>In Partial View :</em></p>
  
  <pre><code>&lt;option value=""&gt;--Select Player--&lt;/option&gt;
  @if (ViewBag.PlayerOptions != null)
  {
      foreach (var item in ViewBag.PlayerOptions) { 
      &lt;option value="@item.Value"&gt;@item.Text &lt;/option&gt;
      }
  }
  </code></pre>
  
  <p><em>In Controller :</em></p>
  
  <pre><code> public ActionResult Create()
      {
          ViewBag.TeamList = new SelectList(GetTeamList(), "IdTeam", "NameTeam");
  ....
              return View(stuModel);
  }
  
  public List&lt;Team&gt; GetTeamList()
      {
          MyDbEntities db = new MyDbEntities();
          List&lt;Team&gt; teamList = db.Teams.ToList();
          return teamList 
      }
  
  
  
      public ActionResult GetPlayerList(int TeamId)
      {
          MyDbEntities db = new MyDbEntities();
          List&lt;Playert&gt; playerList = db.Players.Where(x =&gt; x.IdRefTeam == TeamId).ToList();
          ViewBag.PlayerOptions = new SelectListplayerList "IdPlayer", "PlayerNom");
          return PartialView("PlayerOptionsPartial");
  
      }
  </code></pre>
  
  <p>I think there is a problem with Team's Id wich is not unique... It's always TeamId (coming from "@Html.DropDownListFor(m => m.<strong>TeamId</strong>, ..."</p>
  
  <p>Does somebody have a solution to my problem ?
  How to add dynamically cascading dropdownlist (with 2 or 3 levels)?</p>