I have two <select> controls being used as dropdowns: one with a collection of cities, and the other with a list of hotels. What I would like is that, when the user changes the selected city, the hotels dropdown updates to only show hotels in that city. The drowpdown with the cities is populating successfully; however, I can’t seem to get an event to fire when the city is changed.
Here is my CSHTML for the city dropdown. Note the AutoPostBack="True" tag:
<label asp-for="Destinations" class="col-sm-3">
Destination
</label>
<div class="col-sm-3">
<select runat="server"
id="destinationDropdown"
name="destinationDropdown"
OnSelectedIndexChanged="destinationDropdown_SelectedIndexChanged"
AutoPostBack="True"
style="width:240px">
@foreach (Destination destination in Model.Destinations){
<option value="@destination.DestinationId" id="@destination.DestinationId">@destination.DestinationName</option>
}
</select>
</div>Th
Here is my C# code:
protected void destinationDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//Do work here
...
}
I say that the event isn’t firing, rather than a problem with the hotels dropdown, because I’ve put a breakpoint on the first line of code in the methods, and it doesn’t get hit.
I’ve searched on this site for help, and it seems like the majority of solutions are to include the AutoPostBack="True" tag, which I already have.
Edit: I’ve been informed in the comments that AutoPostBack is a WebForms approach, which is why it doesn’t work in my MVC site. I would still like to know if there is a way to make this work in MVC. I can either edit this question, or start a new one, as appropriate.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you are building a MVC site then runat="server", AutoPostBack="True" and OnSelectedIndexChanged="destinationDropdown_SelectedIndexChanged" attributes will not work. It will only work on a WebForm Site
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0