MVC 模型的 VaryByParam

VaryByParam for MVC Model

我的 MVC 应用程序有以下 search 模型。

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);
}

如果传递的参数是 stringint,我可以设置 VaryByParam = "param",或者如果是多个参数,它将设置为 ';' 分隔值。但是我如何在此处缓存复杂的 model 参数?

根据 MSDN VaryByParam 值应该是

a semicolon-separated list of strings that correspond to query-string values for the GET method, or to parameter values for the POST method.

因此,对于复杂模型,您需要指定其所有属性,并以分号分隔。您还需要考虑您拥有的绑定前缀。由于您的 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"