我正在尝试实施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);
}
});
我缺少什么? everyting工作fne但是在再次调用ajax调用索引方法并清除所有过滤后的数据之后。
答案 0 :(得分:0)
您的代码:
location.reload();
重新加载页面(与用户点击刷新相同)