【问题标题】:why index is invoked after ajax call为什么在ajax调用后调用索引
【发布时间】:2014-03-09 16:11:38
【问题描述】:

我正在尝试实现一个 ajax 调用来过滤产品,如下所示

 public ActionResult Index()
    {
        var model = new ShopViewModel();
        model.RecentlyViewedProductsModel = new RecentlyViewedProductsModel();
        model.SearchProductModel = new SearchProductModel();

        var categories = Entities.Category;

        var rootNodes = Node<Category>.CreateTree(categories, l => l.Id, l => l.ParentId);
        var rootNode = rootNodes.Single();
        model.CategoriesViewModel = rootNode;
        return View(model);
    }
        public PartialViewResult FilterProducts(int? id)
    {
        var model = new List<VehiclePart>();

        if (id != null)
        {
            model.AddRange(Entities.VehiclePart.Where(c => c.ParentId == id));
        }
        else
        {
            model = Entities.VehiclePart.ToList();
        }
        return PartialView("_FilterProducts",model);
    }

在这里我第一次在视图中调用我的方法(Index.cshtml):

<div id="dvFilteredProducts">@{ Html.Action("FilterProducts","Shop",null);}</div>

然后当点击一个类别时,我使用 ajax 调用相同的方法并提供如下参数:

$.ajax({
        type: "POST",
        url: "/Shop/FilterProducts",
        dataType: 'html',
        contentType: "application/json; charset=utf-8",
        beforeSend: function () {
            $('#productLoader').show();
        },
        data: JSON.stringify({id: $("#" + data.node.id).attr("data-cat-id")
        }),
        complete: function () {
            $('#productLoader').hide();
            location.reload();
        },
        success: function (data) {
            if (data.length<10)
                $('#dvFilteredProducts').html("<p>No results...</p>");
            else
                $('#dvFilteredProducts').html(data);
        }
        });

我错过了什么?一切正常,但在再次调用 ajax 调用索引方法并清除所有过滤数据之后。

【问题讨论】:

    标签: jquery ajax asp.net-mvc


    【解决方案1】:

    您的代码:

    location.reload();
    

    重新加载页面(与用户点击刷新相同)

    【讨论】:

    • 我不敢相信我 3 天都看不到 location.reload(),谢谢,我现在测试了它,它可以工作了,谢谢 :)
    • 3 天的开发可能过于密集 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多