I want to get a property of an entity based on passing id's of selected items from cascaded dropdown list...On every change in level 1 or in level 2 of cascaded drop down list, price must be updated.
I have implemented cascaded drop down list. On page load Leaguedropdown (Level 1) populates and second LeagueDivisiondropdown (Level 2) populates when I select any item from Level 1. I have to implement Price Calculator which should calculate price value depending upon selected items from cascaded drop down list, all prices are stored in database and I am using Entity Framework. I am using .change() method and sending getJson request for Level 2 which is working fine. I also want same functionality on Level 1 but I already used .change() method to populate LeagueDivision so I don't have idea how to implement this on Level 1 too.
I will appreciate if some one will guide me and will be thankful.
Here is View and Script
<body>
<p id="message"></p>
@using (Html.BeginForm("PriceCalculatorIndex", "PriceCalculator", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(m => m.SelectedLeague, new { @class = "control-lebel" })
@Html.DropDownListFor(m => m.SelectedLeague, Model.LeagueList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedLeague)
</div>
<div>
@Html.LabelFor(m => m.SelectedLeagueDivision, new { @class = "control-lebel" })
@Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
</div>
<div>
@Html.LabelFor(m => m.Price)
@Html.DisplayFor(m => m.Price)
</div>
}
<!-- JS includes -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
var leaguedivisionUrl = '@Url.Action("Fetchleaguedivision")';
var updatePrice = '@Url.Action("updatePrice")';
var divisions = $('#SelectedLeagueDivision');
$('#SelectedLeague').change(function () {
divisions.empty();
$.getJSON(leaguedivisionUrl, { ID: $(this).val() }, function (data) {
if (!data) {
return;
}
divisions.append($('<option></option>').val());
$.each(data, function (index, item) {
divisions.append($('<option></option>').val(item.Value).text(item.Text));
});
});
});
$('#SelectedLeagueDivision').change(function () {
$.getJSON(updatePrice, { LeagueDivisionId: $(this).val(), LeagueId: $('#SelectedLeague').val() }, function (data) {
if (!data) {
return;
}
alert(data);
//document.getElementById('#message').innerHTML = data;
});
});
</script>
JSON Result to fetch price from database
public JsonResult updatePrice(int LeagueId, int LeagueDivisionId)
{
HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
CalculatorPrice updatedPrice = db.CalculatorPrices
.Where(x => x.LeagueId == LeagueId
&&
x.LeagueDivisionId == LeagueDivisionId)
.FirstOrDefault();
decimal updatedPriceToView = (decimal)updatedPrice.Price;
return Json(updatedPriceToView.ToString(), JsonRequestBehavior.AllowGet);
}
Please try this code. Give reference of my javascript comments where you think I have still mis-understood the question.