2016-03-18 44 views
1

我的search模型适用于我的MVC应用程序。VaryByParam for MVC Model

public class SearchFilters 
{ 
    public SearchFilters() 
    { 
     MinPrice = "10000"; 
     MaxPrice = "8000000"; 
    } 
    public IEnumerable<SelectListItem> Categories { get; set; } 
    public string[] CategoriesId { get; set; } 

    public IEnumerable<SelectListItem> Locations { get; set; } 
    public string[] LocationID { get; set; } 

    public IEnumerable<SelectListItem> Status { get; set; } 
    public string[] StatusID { get; set; } 

    public string MinPrice { get; set; } 
    public string MaxPrice { get; set; } 
} 

现在,当用户搜索任何记录,这将通过model数据传递选定PARAMS和我的GET请求如下:

[HttpGet] 
public ActionResult Search([Bind(Prefix = "searchModel")]SearchFilters smodel) 
{ 
    CategoryViewModel model = new CategoryViewModel(); 
    model = _prepareModel.PrepareCategoryModel("Search", smodel); 
    if (model.projects.Count == 0) 
    { 
     return Json(new { message = "Sorry! No result matching your search", count = model.projects.Count }, JsonRequestBehavior.AllowGet); 
    } 
    return PartialView("_CategoryView", model); 
} 

如果传递的参数是一个stringint,我可以设置VaryByParam = "param"或者如果有多个,它将​​与​​3210分开设置。但是,我如何缓存复杂的model param?

回答

2

按照MSDN的VaryByParam值应该是

对应于查询字符串GET方法 值,或者为POST方法的参数值串的分号分隔的列表。

因此,对于复杂模型,您需要指定以分号分隔的所有属性。您还需要考虑您拥有的绑定前缀。由于您的要求HTTPGET最有可能是这样的:

http://../someUrl?searchModel.MinPrice=1&searchModel.MaxPrice=5&searchModel.CategoriesId=10&searchModel.LocationID=3&searchModel.StatusID=8 

的的VaryByParam值应为:

VaryByParam="searchModel.MinPrice;searchModel.MaxPrice; searchModel.CategoriesId;searchModel.LocationID;searchModel.StatusID" 
+0

谢谢@Alex .. :) –