下拉更改不調用asp.net mvc jquery方法

[英]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。netmvc 3下拉列表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. No change to the URL, which due to the Url.Action("GetBuildings") I assumed would append "/GetBuildings?parkID=2" or something similar.

然而,當我更改“Park”下拉列表的值時,什么也沒有發生。由於URL . action(“GetBuildings”),我以為URL會被追加到“/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!

提前謝謝!

1 个解决方案

#1


2  

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:

您還可以使用$向url添加參數。獲取數據參數:

$.get(queryLink, {parkID : $(this).val()}, function (data) {

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/02/778842db5b058bf411066ed5f47c8553.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com