Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I am new to asp.net mvc , May I know how can I check if the dropdown value is existed in the previous dropdown, if exist then remove the value , if not have the value remain at the dropdown.
Eg: I have cheese , chocolate , strawberry in the dropdown selection , then I select cheese , so I would like cheese to be remove from the next dropdown selection. The dropdown I am using is kendo. Thank you.
The code below in under view

C#
<select class="form-control "
name="Choice[@ViewBag.CakeIndex].Category">
               @foreach (var flavor in ViewBag.Flavour)
               {
                   <option value="@flavor.id">@flavor.name</option>
               }
           </select>


What I have tried:

I had tried using cakes.Items.Remove(flavor); but i am unsure which variable should I use to remove it
Posted
Updated 18-Nov-16 0:03am
v9
Comments
Suvendu Shekhar Giri 16-Nov-16 2:24am    
You can do following to find the corresponding item-
yourDropdown.Items.FindByValue("ValueToFind") //pass the selected value here 
Member 12852998 16-Nov-16 2:25am    
May I know where should i put this code ?
Suvendu Shekhar Giri 16-Nov-16 2:38am    
just before the line where you have written the code to remove the item
F-ES Sitecore 16-Nov-16 5:35am    
What does "previous dropdown" mean? Is it a dropdown on a previous page? On the same page? Are you looking to do this using javascript or on a form submission? Do you just want the option removed from the dropdown or do you want it removed from the ViewBag the dropdown gets its data from?

You haven't given enough of an explanation for what you're looking to do for anyone to give an answer.
Member 12852998 16-Nov-16 20:26pm    
Currently, I am having a button called addcake which enables the user to select more than one cake , so when the user clicked on the addcake button , it would populate another form which is exactly the same as the default one , both of them have a dropdown list that populate the same data , but I would like that if either one of them choose a value , then it would not show up in the other dropdownlist.

Both javascript or on a form submission will be fine , as long that it could work

Removed from the dropdown

1 solution

$('select#select1').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;

var exists = false;
$('select#select2 option').each(function () {
debugger;
if (this.value == valueSelected) {
exists = true;
$("select#select2 option[value='" + valueSelected + "']").remove();
}
});

});
 
Share this answer
 
Comments
Member 12852998 20-Nov-16 20:55pm    
Sorry but the code for the dropdown is only one line which uses the foreach to loop it, may I know where can I define the select1 and select2?
Er. Puneet Goel 21-Nov-16 0:41am    
Select1 and select2 are the dropdowns. For implementation see the fiddler below:
https://jsfiddle.net/erpuneetgoel507/eum8m92v/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900