[英]Dropdown change not calling method asp.net mvc jquery
first off I have used this thread to get where I am now but am having difficulty with my situation.首先,我已经使用这个线程来了解我现在所处的位置,但我的情况有困难。 ASP.NET MVC 3 DropDownList selectedindexchanged ASP.NET MVC 3 DropDownList selectedindexchanged
I wish to select a value from one dropdown list which will then reduce the available options on a dropdown below it.我希望从一个下拉列表中选择一个值,然后将减少其下方下拉列表中的可用选项。
Similar to the thread above, I am trying to use jQuery to accomplish this like so:与上面的线程类似,我正在尝试使用 jQuery 来完成此操作:
$('#Park').change(function () {
var queryLink = 'Url.Action("GetBuildings")';
if ($(this).val() != '') {
queryLink += '?parkID=' + $(this).index;
}
$.get(queryLink, function (data) {
$b = $('#Building');
$op = $('<option></option>');
$b.empty();
$.each(data, function (value) {
$b.append($op.attr("value", value));
});
});
});
and here is the GetBuildings function in the AvailabilityController:这是 AvailabilityController 中的 GetBuildings 函数:
public ActionResult GetBuildings(int parkID)
{
var buildQry = from b in systemDB.Buildings
where b.ParkID == parkID
orderby b.BuildingName
select b.BuildingName;
string temp = "";
foreach (string b in buildQry)
{
temp += b + ';';
}
temp = temp.Substring(0, temp.Length - 1);
string[] buildings = temp.Split(';');
return View(buildQry);
}
However, when I change the value of the "Park" dropdown, nothing happens.但是,当我更改“Park”下拉列表的值时,没有任何反应。 No change to the URL, which due to the Url.Action("GetBuildings") I assumed would append "/GetBuildings?parkID=2" or something similar.没有更改 URL,这是由于 Url.Action("GetBuildings") 我认为会附加 "/GetBuildings?parkID=2" 或类似的内容。
I'm not sure why it isn't at least doing something as I have made it as similar to the original thread as possible which worked correctly.我不确定为什么它至少没有做一些事情,因为我已经使它尽可能与正常工作的原始线程相似。
Thanks in advance!提前致谢!
For me this line is the issue:对我来说,这一行是问题:
var queryLink = 'Url.Action("GetBuildings")';
If you are doing that on the same view you can parse the asp tag by doing (note the @ ):如果您在同一视图上执行此操作,则可以通过执行以下操作来解析 asp 标记(注意@ ):
var queryLink = '@Url.Action("GetBuildings")';
But i don't recommend that approach, you get get away cleaner than that with a html5 data-attribute但我不推荐这种方法,你会比使用 html5 数据属性更干净
EDIT: using data-attribute example.编辑:使用数据属性示例。 Add to the dropdown element an attribute like:向下拉元素添加一个属性,如:
<select id="Park" data-action-url='@Url.Action("GetBuildings")'>
on your js you get the url:在你的 js 上你得到了 url:
var queryLink = $(this).attr('data-action-url');
You can also add the parameter to the url by using the $.get data parameter:您还可以使用 $.get 数据参数将参数添加到 url:
$.get(queryLink, {parkID : $(this).val()}, function (data) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.