Eric Eric - 4 years ago 195
Javascript Question

MVC DropDownFor second select not firing jquery change event

I'm new to MVC and trying to learn it.

On my view I have a partialview with a dropdown list:

<div class="panel-body col-xs-12 col-sm-12 col-md-12 col-lg-12" id="LocationsList">
<div class="row row-spacing">
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4">
@Html.LabelFor(m => m.locationType.Name, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
@Html.DropDownListFor(m => m.locationType.LocationTypeID,
new SelectList(Model.LocationTypes,
"LocationTypeID",
"Name"),
htmlAttributes: new { @class = "form-control dropdown", id = "cbLocationTypes" })
@Html.ValidationMessageFor(m => m.locationType.LocationTypeID, "", new { @class = "text - danger" })
</div>
</div>

<div class="row">
<div class="col-xs-10 col-sm-10 col-md-10 col-lg-10" style="border-bottom: 2px solid darkgray;">
<span class="caption h4 text-info">Locations</span>
</div>
<div class="col-xs-2 col-sm-2 col-md-2 col-lg-2" style="border-bottom: 2px solid darkgray;">
<span class="caption h4 text-info">&nbsp;</span>
</div>
</div>
@if (Model != null)
{
var m = Model.Locations;
if (m.Count > 0)
{
foreach (var item in m)
{
<div class="row" style="margin-bottom: 2px; margin-top: 2px;">
<div class="col-sx-9 col-sm-9 col-md-9 col-lg-9">
@Html.DisplayFor(modelItem => item.Name)
</div>
<div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 no-padding no-margin" style="width: 60px;">
<a href="@Url.Action("SelectLocation", "Locations", new { ID = item.LocationID })" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-pencil halfsize"></span>
</a>
<a href="@Url.Action("DeleteLocation", "Locations", new { ID = item.LocationID })" class="btn btn-default btn-xs btn-red" ID="btnDeleteLocation" clientID="btnDeleteLocation">
<span class="glyphicon glyphicon-remove halfsize no-padding no-margin"></span>
</a>
</div>
<div class="col-sx-2 col-sm-2 col-md-2 col-lg-2 no-padding no-margin" style="width: 55px;">
@if (item.Position > 1)
{
<a href="@Url.Action("MoveUp", "Locations", new { ID = item.LocationID })" class="btn btn-default btn-xs btn-green">
<span class="glyphicon glyphicon-chevron-up halfsize"></span>
</a>
}
else
{
<span style="padding-left: 25px;"></span>
}
@if (item.Position < m.Count())
{
<a href="@Url.Action("MoveDown", "Locations", new { ID = item.LocationID })" class="btn btn-default btn-xs btn-green">
<span class="glyphicon glyphicon-chevron-down halfsize no-padding no-margin"></span>
</a>
}
</div>
</div>
}
}
else
{
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<span class="label label-primary h4">No locations available</span>
</div>
}
}
else
{
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<span class="label label-primary h4">No locations available</span>
</div>
}
</div>


On the Index view where this partial is placed I have the Jquery code:

$('#cbLocationTypes').on('change', function () {
var id = $('Select#cbLocationTypes').val();
$.ajax({
url: '/locations/RefreshList/' + id.toString(),
type: 'GET',
success: function (data) { $('#LocationsList').html(data); } })
.done(function() {
$('#LocationsList option[value='+id.toString()+']').prop("selected", "selected");
})
});


In my controller I have the code to run when the dropdown selection changes:

[HttpGet]
public ActionResult RefreshList(int id)
{
LocationsViewModel vm = new LocationsViewModel();
vm.Locations = db.Locations.OrderBy(m => m.Position).Where(m=>m.LocationTypeID== id).ToList();
vm.LocationTypes = db.LocationType.OrderBy(m => m.Name).ToList();
vm.Location = new Locations();
vm.Location.LocationID = -1;
vm.Location.Name = "";
vm.Location.LocationTypeID = 1;

return PartialView("_LocationsListPartial", vm);
}


The first call works perfectly.
The table on the partial is updated.

But....

When I change the selection of the dropdownlist again, nothing happens.
The jquery event is not responding (checked with break in developer panel).

What did I do wrong/ what am I missing?

Answer Source

You have to use delegated event as you are updating the DOM with new elements with every event execution:

$('#LocationsList').on('change', '#cbLocationTypes', function () {
     // code block
});

This syntax is:

$(staticParent).on(event, selector, callback);

Here $(staticParent) should be closest parent element which was available on the page at page load. Although the event can be delegated to $(document) too but not recommended in large size of DOM structure. This could cause slowness.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download