如何根据级联下拉列表选择的值从数据库中获取 属性 值?
How to get a property value from database based on cascaded drop down list selected values??
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.
我实现了级联下拉列表。在页面加载时 Leaguedropdown(级别 1) 填充和第二个 LeagueDivisiondropdown(级别 2) 当我 select 来自级别 1 的任何项目时填充。我必须实施 价格计算器,它应该根据级联下拉列表中的 selected 项目计算价格值,所有价格都存储在数据库中,我正在使用 Entity Framework.
我正在使用 .change() 方法并发送工作正常的 2 级 getJson 请求。我也想在级别 1 上使用相同的功能,但我已经使用 .change() 方法来填充 LeagueDivision,所以我也不知道如何在级别 1 上实现它。
如有高人指点,将不胜感激
这是视图和脚本
<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 从数据库中获取价格的结果
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);
}
请尝试此代码。参考我的 javascript 评论,你认为我仍然误解了这个问题。
<script type="text/javascript">
var leaguedivisionUrl = '@Url.Action("Fetchleaguedivision")';
var updatePrice = '@Url.Action("updatePrice")';
var divisions = $('#SelectedLeagueDivision');
function bindLeagueDivisionData(data) {
divisions.empty();
$.each(data, function(index, item) {
divisions.append($('<option></option>').val(item.Value).text(item.Text));
});
}
function getUpdatedPrice(leagueId, divisionId) {
$.getJSON(updatePrice, {
LeagueDivisionId: divisionId,
LeagueId: leagueId
}, function(data) {
if (!data) {
return;
}
alert(data);
//document.getElementById('#message').innerHTML = data;
});
}
$('#SelectedLeague').change(function() {
if (divisions.find("option").length == 0) {
/*The division list is empty, fetch the divisions*/
$.getJSON(leaguedivisionUrl, {
ID: $(this).val()
}, function(data) {
if (!data) {
return;
}
/*Once division data is bound, get the price for the league and first selected division. If you choose to leave first division as empty, your wish; just remove the contents of done in that case*/
$.when(bindLeagueDivisionData(data)).done(function() {
getUpdatedPrice($(this).val(), $('#SelectedLeagueDivision'));
});
});
} else {
/*Divisions are already present just get the price for the combination*/
getUpdatedPrice($(this).val(), $('#SelectedLeagueDivision'));
}
});
$('#SelectedLeagueDivision').change(function() {
/*Get the price for the combination*/
getUpdatedPrice($('#SelectedLeague').val(), $(this).val());
});
</script>
I have implemented successfully in this way.
@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>
<label>
price :
</label>
<span id="spPrice"></span>
</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">
$(function () {
LoadDivisionsAndPrice()
});
var leaguedivisionUrl = '@Url.Action("Fetchleaguedivision")';
var updatePrice = '@Url.Action("updatePrice")';
var divisions = $('#SelectedLeagueDivision');
$('#SelectedLeague').change(function () {
LoadDivisionsAndPrice();
});
$('#SelectedLeagueDivision').change(function () {
LoadPrice();
});
function LoadPrice() {
$.getJSON(updatePrice, { LeagueId: $('#SelectedLeague').val(), LeagueDivisionId: $('#SelectedLeagueDivision').val() }, function (data) {
if (!data) {
return;
}
document.getElementById('spPrice').innerText = data.Price;
});
}
function LoadDivisionsAndPrice()
{
divisions.empty();
$.getJSON(leaguedivisionUrl, { ID: $('#SelectedLeague').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));
});
LoadPrice();
});
}
</script>
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.
我实现了级联下拉列表。在页面加载时 Leaguedropdown(级别 1) 填充和第二个 LeagueDivisiondropdown(级别 2) 当我 select 来自级别 1 的任何项目时填充。我必须实施 价格计算器,它应该根据级联下拉列表中的 selected 项目计算价格值,所有价格都存储在数据库中,我正在使用 Entity Framework. 我正在使用 .change() 方法并发送工作正常的 2 级 getJson 请求。我也想在级别 1 上使用相同的功能,但我已经使用 .change() 方法来填充 LeagueDivision,所以我也不知道如何在级别 1 上实现它。
如有高人指点,将不胜感激
这是视图和脚本
<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 从数据库中获取价格的结果
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);
}
请尝试此代码。参考我的 javascript 评论,你认为我仍然误解了这个问题。
<script type="text/javascript">
var leaguedivisionUrl = '@Url.Action("Fetchleaguedivision")';
var updatePrice = '@Url.Action("updatePrice")';
var divisions = $('#SelectedLeagueDivision');
function bindLeagueDivisionData(data) {
divisions.empty();
$.each(data, function(index, item) {
divisions.append($('<option></option>').val(item.Value).text(item.Text));
});
}
function getUpdatedPrice(leagueId, divisionId) {
$.getJSON(updatePrice, {
LeagueDivisionId: divisionId,
LeagueId: leagueId
}, function(data) {
if (!data) {
return;
}
alert(data);
//document.getElementById('#message').innerHTML = data;
});
}
$('#SelectedLeague').change(function() {
if (divisions.find("option").length == 0) {
/*The division list is empty, fetch the divisions*/
$.getJSON(leaguedivisionUrl, {
ID: $(this).val()
}, function(data) {
if (!data) {
return;
}
/*Once division data is bound, get the price for the league and first selected division. If you choose to leave first division as empty, your wish; just remove the contents of done in that case*/
$.when(bindLeagueDivisionData(data)).done(function() {
getUpdatedPrice($(this).val(), $('#SelectedLeagueDivision'));
});
});
} else {
/*Divisions are already present just get the price for the combination*/
getUpdatedPrice($(this).val(), $('#SelectedLeagueDivision'));
}
});
$('#SelectedLeagueDivision').change(function() {
/*Get the price for the combination*/
getUpdatedPrice($('#SelectedLeague').val(), $(this).val());
});
</script>
I have implemented successfully in this way.
@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>
<label>
price :
</label>
<span id="spPrice"></span>
</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">
$(function () {
LoadDivisionsAndPrice()
});
var leaguedivisionUrl = '@Url.Action("Fetchleaguedivision")';
var updatePrice = '@Url.Action("updatePrice")';
var divisions = $('#SelectedLeagueDivision');
$('#SelectedLeague').change(function () {
LoadDivisionsAndPrice();
});
$('#SelectedLeagueDivision').change(function () {
LoadPrice();
});
function LoadPrice() {
$.getJSON(updatePrice, { LeagueId: $('#SelectedLeague').val(), LeagueDivisionId: $('#SelectedLeagueDivision').val() }, function (data) {
if (!data) {
return;
}
document.getElementById('spPrice').innerText = data.Price;
});
}
function LoadDivisionsAndPrice()
{
divisions.empty();
$.getJSON(leaguedivisionUrl, { ID: $('#SelectedLeague').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));
});
LoadPrice();
});
}
</script>