0

I have 2 dropdownlists: 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.

Next, I want to dynamically add a new line of this cascading dropdownlist. Currently, I'm able 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".

workflow

======================================================

EDITION :

2 images added to clarify my problem => Database and Workflow

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.

======================================================

My Code:

In View:

                        <form method="post">
                <a style="margin: 10px 0;" href="javascript:addRow();">Add Row</a>
                <table id="formTable">
                    <thead>
                    <tr>
                        <th>Team</th>
                        <th>Player</th>
                    </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                @if (ViewBag.TeamList != null)
                                {
                                    @Html.DropDownListFor(m => m.TeamId, ViewBag.TeamList as SelectList, "-- Select Team--", new { @class = "form-control" })

                                }
                            </td>
                            <td>
                                @Html.DropDownListFor(m => m.PlayerId, new SelectList(""), "--Select Player--", new { @class = "form-control" })
                            </td>
                        </tr>                           
                    </tbody>
                </table>
            </form>





@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    @Styles.Render("~/Content/css")

    @Styles.Render("~/Script/js")

<!-- Cascading dropdownlist-->
<script>
$(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);
            }
        })
    })
})
</script>

<!--  Add new row-->
<script type="text/javascript">// <![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");
    }
// ]]></script>

In Partial View:

<option value="">--Select Player--</option>
@if (ViewBag.PlayerOptions != null)
{
    foreach (var item in ViewBag.PlayerOptions) { 
    <option value="@item.Value">@item.Text </option>
    }
}

In Controller:

 public ActionResult Create()
    {
        ViewBag.TeamList = new SelectList(GetTeamList(), "IdTeam", "NameTeam");
....
            return View(stuModel);
}

public List<Team> GetTeamList()
    {
        MyDbEntities db = new MyDbEntities();
        List<Team> teamList = db.Teams.ToList();
        return teamList 
    }



    public ActionResult GetPlayerList(int TeamId)
    {
        MyDbEntities db = new MyDbEntities();
        List<Playert> playerList = db.Players.Where(x => x.IdRefTeam == TeamId).ToList();
        ViewBag.PlayerOptions = new SelectListplayerList "IdPlayer", "PlayerNom");
        return PartialView("PlayerOptionsPartial");

    }

I think there is a problem with Team's Id which is not unique... It's always TeamId (coming from "@Html.DropDownListFor(m => m.TeamId, ..."

Does somebody have a solution to my problem? How to add dynamically cascading dropdownlist (with 2 or 3 levels)?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
SamSoul
  • 1
  • 2
  • Can you clarify the question. Are you wanting 4 drop down lists with #4 depending on results from #3 which depends on results from #2 which depends on #1? If so, you just need to add additional ajax lookups for each DDL based on the selected value of the previous one. – melkisadek Jan 19 '18 at 16:32
  • There are multiple issues with your code. Suggest you study the code in [this DotNetFiddle](https://dotnetfiddle.net/1bPZym) –  Jan 19 '18 at 22:44
  • @ melkisadek : I've edit my post and added 2 images : one for database, the other for workflow As you can see ,the content in panel 9 for the second player's selection is wrong. The second player dropdownlist doesn't take for reference the Id of the second selected Team (team3) but the Id of the first Team's dropdownlist – SamSoul Jan 20 '18 at 21:04
  • @ Stephen Muecke: Thank you for your reply. But that's not exactly what I want. I've edited my post and added 2 images to explain my problem. – SamSoul Jan 20 '18 at 21:26
  • Imagine a table with 2 columns and in each a dropdownlist : one for Team, one for Player. First, I select a Team and next a player in this Team. In a second time, I want to be abble to add a new line (thanks to a button or an actionlink) in order to select an other combination of Team / Player. Currently that's not what I get because when I add a new row, it's ok for the content of Team's dropdownlist but not for the player's dropdownlist : it display the same list of players that I've in the first line of my table – SamSoul Jan 20 '18 at 21:26
  • @SamSoul, I was pointing out that your code for cascading dropdownlists simply awful and full of issues that will cause failure. And so is the code in your edit for adding new 'rows' so I suggest you study the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) –  Jan 21 '18 at 00:57

0 Answers0