1

我的应用程序有以下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);
}

如果传递的参数是 astringint,我可以设置VaryByParam = "param",或者如果是多个,则使用';'分隔值设置。但是我将如何缓存复杂的model这里缓存复杂的参数?

4

1 回答 1

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"
于 2016-03-18T18:32:45.810 回答