Hi,
After many hours I finally solved it. I don't know if this is a good solution but I thought I should share it anyway.
In controller:
public ActionResult LoadCourse(SettingsModel model)
{
...load album model depending on artist here
return Json(model.Albums, JsonRequestBehavior.AllowGet);
}
public ActionResult LoadCourse(SettingsModel model)
{
...load title model depending on album here
return Json(model.Title, JsonRequestBehavior.AllowGet);
}
The JavaScript:
<script type="text/javascript">
var Urls = {
UrlArtist: '@Url.Action("LoadArtists", "Settings")',
UrlTitle: '@Url.Action("LoadTitles", "Settings")'
}
$(function () {
$("#artistId").change(function () {
var selectedItem = $(this).val();
var statesProgress = $("#ajaxLoader");
statesProgress.show();
$.ajax({
cache: false,
type: "POST",
url: Urls.UrlArtist,
data: { "ArtistName": selectedItem },
success: function (data) {
$("#albumId").html("");
var $select = $('#albumId');
$.each(data, function (i, item) {
$('<option>', {
value: item.Value
}).html(item.Text).appendTo($select);
});
$.ajax({
cache: false,
type: "POST",
url: Urls.UrlTitle,
data: { "AlbumName": selectedItem },
success: function (data) {
$("#teelist").html("");
var $select = $('#teelist');
$.each(data, function (i, item) {
$('<option>', {
value: item.Value
}).html(item.Text).appendTo($select);
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve titles.');
}
});
statesProgress.hide();
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve albums.');
statesProgress.hide();
}
});
});
});
</script>
Regards,
Lars