【发布时间】: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