我有一个 MVC 项目。我正在使用 jQuery ajax post 对我的产品进行排序。下面的控制器。
[HttpGet]
public ActionResult Category() //this is first level. And its working....
{
var result = Url.RequestContext.RouteData.Values["categoryName"].ToString();
const string sorttype = "A/Z";
var liste = _productRepository.GetProductByCategoryName(result);
List<Product> deger;
switch (sorttype)
{
case "A/Z":
deger = liste.OrderBy(s => s.ProductName).ToList();
break;
}
var modelim = new CategoryViewModel
{
Catname = result,
Products = deger
};
return View(modelim);
}
[HttpPost]
public ActionResult Category(string sortingName, string catName)//get to jquery post.
{
string sorttype = sortingName;
var liste = _productRepository.GetProductByCategoryName(catName);
List<Product> deger = null;
switch (sorttype)
{
case "A/Z":
deger = liste.OrderBy(s => s.ProductName).ToList();
break;
case "Z/A":
deger = liste.OrderByDescending(s => s.ProductName).ToList();
break;
case "Fiyat Artan":
deger = liste.OrderBy(s => s.ProductPrice).ToList();
break;
case "Fiyat Azalan":
deger = liste.OrderByDescending(s => s.ProductPrice).ToList();
break;
}
var modelim = new CategoryViewModel
{
Catname = catName,
Products = deger //this is new ordered list!!!!
};
return View(modelim);
}
我已更改订购的产品列表。我将新模型发送到我的剃须刀页面。但一切都没有改变。???怎么了?非常感谢。
下面是我的 jQuery 文件,
$('#sortList').on({
change:function() {
var result = $('#sortList option:selected').text();
var catname = $('#catName').text();
var message = { 'sortingName': result, 'catName': catname };
$.ajax({
url: '/Category/Category',
type: 'POST',
data: JSON.stringify(message),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
if (data.error) {
} if (data.success) {
}
}
});
}
});