【问题标题】:JQuery - does not fire MVC Controller ActionJQuery - 不触发 MVC 控制器动作
【发布时间】:2009-05-06 03:11:48
【问题描述】:

我有以下 JQuery 代码:

// When the document is ready, start firing our AJAX
$(document).ready(function() {
    function showValues() {
        var str = $("form").serialize();
        $("#results").text(str);
    }

    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    //showValues();

    // Bind actions...
    $("#navIndex a").bind("click", function(e) { updateNavIndex($(this).attr('href')); });
    $("#navPrevNext a").bind("click", function(e) { updateNavPrevNext($(this).attr('href')); });
    $("#ItemsPerPage").bind("change", function(e) { updateAll(); });
    $(":checkbox, :radio").bind("change", function(e) { updateAll(); });

    $("#navIndex a").click(function() {
        // switch class type...
        $("#navIndex a").removeClass('selected');
        $("#navIndex span").removeClass('selected');
        $("#navIndex a").addClass('notselected');
        $("#navIndex span").addClass('notselected');
        $(this).removeClass('notselected');
        $(this).addClass('selected');
        $(this).parent().removeClass('notselected');
        $(this).parent().addClass('selected');

        // Get navigation index...
        var navIndex = $(this).attr('href');

        this.blur();
        return true;
    });
    $("#navPrevNext a").click(function() {
        // Get navigation index...
        var navIndex = $(this).attr('href');

        this.blur();
        return true;
    });
});

// Use the getJSON method to call our JsonResult action
var retrieveProductData = function(path, productGroup, productType, itemsPerPage, pageIndex, filter, fnHandleCallback) {
    $.getJSON(path
             , { productGroup: productGroup }
             , { productType: productType }
             , { itemsPerPage: itemsPerPage }
             , { pageIndex: pageIndex }
             , { filter: filter }
             , function(data) { fnHandleCallback(data); });
};

// Use the getJSON method to call our JsonResult action
var retrieveMenuData = function(path, productGroup, productType, itemsPerPage, pageIndex, filter, fnHandleCallback) {
    $.getJSON(path
             , { productGroup: productGroup }
             , { productType: productType }
             , { itemsPerPage: itemsPerPage }
             , { pageIndex: pageIndex }
             , { filter: filter }
             , function(data) { fnHandleCallback(data); });
};

// The path parameter is our JSON controller action
function updateNavIndex(pageIndex) {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();

    retrieveMenuData("/CatalogAjaxController/UpdateNavigation"
                    , productGroup
                    , productType
                    , itemsPerPage
                    , pageIndex
                    , filters
                    , handleMenuData);
    retrieveProductData("/CatalogAjaxController/UpdateNavigation"
                       , productGroup
                       , productType
                       , itemsPerPage
                       , pageIndex
                       , filters
                       , handleProductData);
}

// The path parameter is our JSON controller action
function updateNavPrevNext(pageIndex) {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();

    retrieveMenuData("/CatalogAjaxController/UpdateNavigation"
                    , productGroup
                    , productType
                    , itemsPerPage
                    , pageIndex
                    , filters
                    , handleMenuData);
    retrieveProductData("/CatalogAjaxController/UpdateNavigation"
                       , productGroup
                       , productType
                       , itemsPerPage
                       , pageIndex
                       , filters
                       , handleProductData);
}

// The path parameter is our JSON controller action
function updateAll() {
    var filters = $("form").serialize();
    var productGroup = $("#valProductGroup").attr('title');
    var productType = $("#valProductType").attr('title');
    var itemsPerPage = $("#ItemsPerPage").val();

    retrieveMenuData("/CatalogAjaxController/UpdateNavigation"
                    , productGroup
                    , productType
                    , itemsPerPage
                    , pageIndex
                    , filters
                    , handleMenuData);
    retrieveProductData("/CatalogAjaxController/UpdateProducts"
                       , productGroup
                       , productType
                       , itemsPerPage
                       , pageIndex
                       , filters
                       , handleProductData);
}

// Ok, now we have the JSON data, we need to do something with it.  I'm adding it to another dropdown.  
function handleMenuData(data) {
    $("#otherDropDownId > option").remove();
    for (d in data) {
        var item = data[d];
        $("#otherDropDownId").append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
    }
}

// Ok, now we have the JSON data, we need to do something with it.  I'm adding it to another dropdown.  
function handleProductData(data) {
    $("#otherDropDownId > option").remove();
    for (d in data) {
        var item = data[d];
        $("#otherDropDownId").append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
    }
}

我的控制器看起来像:

public class CatalogAjaxController : Controller
{
    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public JsonResult UpdateNavigation(string productGroup, string productType, int itemsPerPage, string pageIndex, string filters)
    {
        int pIndex = Convert.ToInt32(pageIndex.Remove(0, 1));

        ProductCatalogBrowserModel myModel;
        myModel = new ProductCatalogBrowserModel(productGroup, productType, pIndex, itemsPerPage);

        return new JsonResult() { Data = myModel.ProductDetailMenu.ToArray() };
    }

    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public JsonResult UpdateProducts(string productGroup, string productType, int itemsPerPage, string pageIndex, string filters)
    {
        int pIndex = Convert.ToInt32(pageIndex.Remove(0, 1));

        ProductCatalogBrowserModel myModel;
        myModel = new ProductCatalogBrowserModel(productGroup, productType, pIndex, itemsPerPage);

        return new JsonResult() { Data = myModel.ProductDetail.ToArray() };
    }

}

我可以在 JS 脚本的 3 个更新函数中的任何一个中捕获断点,但它根本不会放入控制器中。我错过了什么吗?

【问题讨论】:

    标签: jquery asp.net-mvc


    【解决方案1】:

    我认为您错过了如何构建 Ajax 参数。

    $.getJSON(
        url,
        {
            'dataVal1': data1,
            'dataVal2': data2
        },
        myCallBackHandler
    );
    

    但是,这会传递一个 GET 请求,并且您可能需要一个 POST。

    您可以通过基本的 Ajax 调用来做到这一点:

    $.ajax({
        url: thePath,
        type: 'POST',
        data: {
            dataVal1: data1,
            dataVal2: data2
        },
        success: successHandler,
        failure: failureHandler
    });
    

    您可以在这里找到更多的使用场景:

    http://docs.jquery.com/Ajax/jQuery.ajax#options

    【讨论】:

    • 好的。我认为我理解,但不确定该调用的确切位置。我猜它应该放在“var retrieveXYZ”定义中?
    • 看看你的代码,我会说是的,$.ajax() 调用会进入你不同的检索函数。
    • 我不断收到错误“类型未知”。这就是我对检索代码所做的: var retrieveMenuData = function(path, productGroup, productType, itemsPerPage, pageIndex, filter, fnHandleCallback) { $.ajax( path , type , { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage , pageIndex: pageIndex, filter: filter } , function(data) { fnHandleCallback(data); }); };然后,在我的更新方法中,我有: retrieveMenuData("/CatalogAjaxController/UpdateNavigation" , "Post" , {"productGroup":productGroup , "productType":productType ...
    • 好的。我正在放弃检索定义(现在)并且几乎可以正常工作。我收到一条错误消息,提示“数据未定义”。函数 updateNavIndex(pageIndex) { var filters = $("form").serialize(); ...(其他声明)... var itemsPerPage = $("#ItemsPerPage").val(); $.ajax({ path: "/CatalogAjaxController/UpdateNavigation" , type: 'POST' , data: { productGroup: productGroup, productType: productType, itemsPerPage: itemsPerPage, pageIndex: pageIndex, filters: filters } , 成功: handleMenuData(data) });
    • 是的,看看你的两个 cmets,你似乎走在了正确的轨道上,你只需要命名你的论点。我看到您有一个“成功”处理程序,您可能还需要一个“失败”处理程序来进行故障排除。在进行 Ajax 开发时,Firebug 绝对是您的朋友。如果您已经在使用 Ext 3,并且必须使用不同的浏览器,请务必查看新的 Debug 组件。
    【解决方案2】:

    你的 JS 是 GETting,控制器动作只接受一个 POST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多