0

I am using a loop to create multi DropDownList in the same view. As a result of the loop, two lists are created. However the problem is when user select an item from one list, the other list will change its selection to be the same selection from the other one.
So, how do I avoid that, and make selection process separated from a list to another?
Here is the code generate the list

@Html.DropDownList("selectedDays", listItems, new { @class = "form-control", id="Day"+item.id })

EDIT:
The ListItem Code is this

List<SelectListItem> listItems = new List<SelectListItem>();

listItems.Add(new SelectListItem
{

    Text = "1 day",
    Value = "1"
});
listItems.Add(new SelectListItem
{

    Text = "3 days",
    Value = "3"
});
listItems.Add(new SelectListItem
{
    Selected = true,
    Text = "7 days",
    Value = "7"
});
listItems.Add(new SelectListItem
{

    Text = "15 days",
    Value = "15"
});
listItems.Add(new SelectListItem
{

    Text = "30 days",
    Value = "30"
});


and This is the loop

@{
foreach (var item in companyList)
{
<div class="panel-body">

                                            <form class="form-horizontal" role="form" action="/Admin/Telecom" method="post">
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">Payment View</label>
                                                    <div class="col-sm-6">

                                                        @Html.DropDownList("selectedDays", listItems, new { @class = "form-control", id="Day"+item.id })
                                                    </div>

                                                    <div class="col-sm-3">
                                                        <button type="submit" name="btnView" value="@item.TelecomCompanyName" class="btn btn-primary btn-rounded w-md waves-effect waves-light m-b-5">change</button>
                                                    </div>
                                                </div>
                                            </form>


                                    </div>
}
5
  • Provide the code for the entire loop. Commented Jul 4, 2017 at 6:16
  • it is too large, do we need that?? @RacilHilan Commented Jul 4, 2017 at 6:21
  • Yes, I'm afraid you do. First of all a too large loop by itself is an indication of bad code. Anyway, you don't need to post it all, just post the lines related to the list and the loop line. We need to find why the too lists are linked. Commented Jul 4, 2017 at 7:16
  • are you sure each list item is having different id's Commented Jul 4, 2017 at 7:20
  • Did you mean to create one form with two lists? Because your code is creating two forms with one list each. If that's what you want, then fine, otherwise you have to take the code for the form out of the loop. and if this is the entire look, it's not too bad :-) Commented Jul 4, 2017 at 8:01

2 Answers 2

0

You shouldn't reuse listItems between DropDownLists. After selecting a value on one list, element will be Selected on all of them. Create separate lists for each DropDownList.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but unfortunately it didn't work @borkovski
Take a look here: stackoverflow.com/questions/18880662/… Maybe it'll match your case.
0

You are dynamically generating more dropdownlists with the same id.
They should have different ids to behave correctly.

@Html.DropDownList("selectedDays" + item.id, listItems, new { @class = "form-control", id="Day"+item.id })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.