0

我已经广泛研究了这个问题,但无法找出问题所在。我正在尝试使用 MVC4 和 Jquery 为我拥有的页面实现级联下拉菜单,无论我在线使用什么解决方案,我的 ActionResult 参数始终为空。

这是可以正常调用的Javascript:

<script type="text/javascript">
$('.clientnames').on('change', function (e) {

    // Retrieve the filter url from the data-filter-url attribute
    var filterUrl = $(this).attr('data-filter-url');

    // Grab the id of the selected item
    var selectedId = $(this).val();
    window.alert("filterurl:" + filterUrl + "    selectedID:" + selectedId);

    // Grab the container that should be updated
    var updateContainer = $(this).attr('data-update-container');

    // Grab the id of new dropdown
    var dataUpdateId = $(this).attr('data-update-id');

    var completeUrl = filterUrl + selectedId;

    $.get(completeUrl, function (r) {

        // Load Partial View using the URL from the data-filter-url attribute
        $(updateContainer).html(r);

        // This would be used if you wanted to trigger the new dropdown to filter another
        if (dataUpdateId != null) {
            // Trigger the change event after it is loaded
            $(dataUpdateId).trigger('change');
        }
    });
});

控制器函数,它被调用但值未成功传入:

 public ActionResult ImportList(string selectedId)
     {
         //DO COOL STUFF
     }

和观点:

 @Html.DropDownListFor(model => model.Clients.DropdownItems, 
         new SelectList(Model.Clients.DropdownItems, "ItemID","ItemName"),
                        new
                        {
                            @class = "clientnames",
                            data_filter_url = Url.Content("~/import/importlist/"),
                            data_update_container = ".dropdown-container",
                            data_update_id = "#SecondItemID"
                        }
)

我非常遵循此页面上的建议:http ://www.sidecreative.com/Blog/Entry/Cascading-dropdown-lists-with-MVC4-and-jQuery

尽管我尝试了几种不同的解决方案,但每次该值仍然为空。我对 MVC 和 Jquery 很陌生,所以任何建议都将不胜感激。

4

1 回答 1

0

尝试像这样传递选定的值:

$('.clientnames').on('change', function (e) {

    // Retrieve the filter url from the data-filter-url attribute
    var filterUrl = $(this).attr('data-filter-url');

    // Grab the id of the selected item
    var selectedId = $(this).val();
    window.alert("filterurl:" + filterUrl + "    selectedID:" + selectedId);

    // Grab the container that should be updated
    var updateContainer = $(this).attr('data-update-container');

    // Grab the id of new dropdown
    var dataUpdateId = $(this).attr('data-update-id');

    $.get(filterUrl, { selectedId: selectedId }, function (r) {

        // Load Partial View using the URL from the data-filter-url attribute
        $(updateContainer).html(r);

        // This would be used if you wanted to trigger the new dropdown to filter another
        if (dataUpdateId != null) {
            // Trigger the change event after it is loaded
            $(dataUpdateId).trigger('change');
        }
    });
});

您的控制器操作接受一个名为 的参数selectedId,并注意它是如何在$.get函数中传递的:

$.get(filterUrl, { selectedId: selectedId }, function (r) {
    ...
});

您还可以following post查看在 ASP.NET MVC 中实现级联下拉菜单的其他示例。

于 2013-08-27T15:29:23.183 回答