用于MVC模型的VaryByParam

时间:2016-03-18 18:11:54

标签: asp.net asp.net-mvc asp.net-mvc-4 outputcache varybyparam

我的search应用程序的MVC模型低于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

现在,当用户搜索任何记录时,它会通过[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); } 数据传递选定的参数,我的获取请求如下:

string

如果传递的参数是intVaryByParam = "param",我可以设置';',如果是多个,则会设置model个分隔值。但是我如何在这里缓存复杂的DB::DB()参数?

1 个答案:

答案 0 :(得分:2)

根据MSDN VaryByParam值应

  

以分号分隔的字符串列表,对应于query-string   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"