asp.net-mvc-3asp.net-caching

OutputCache varying by a complex object property


I have a controller action that receives a complex object as a parameter, I need the OutputCache to vary by one of the properties of this complex object. Is this possible? How?


Solution

  • if you have a model like

    public class person{
     public string Name {get;set;}
     public string location {get;set;}
    } 
    

    and in the (strongly typed)view you have a form

     @model Person
    
     @Html.BeginForm(){
      @Html.TextBoxFor(x=>x.Name)
      @Html.TextBoxFor(x=>x.location)
     }
    

    and you submit the form to an ActionResult savePerson, with varying signature like

    public ActionResult savePerson(Person p){
     // p.Name
     // p.location
    
    }
    

    or

    public ActionResult savePerson(string Name, string location){
    
    }
    

    therefore i think if you annotate the ActionResult like

    [OutputCache(Duration=3600, VaryByParam="Name")]
    public ActionResult savePerson(Person p)
    {
        //
        return View();
    }
    

    it will do for you, or if you have a complex model like

    public class person{
     public string Name {get;set;}
     public Location loc {get;set;}
    } 
    public class Location{
      public string address
    }
    

    try

    [OutputCache(Duration=3600, VaryByParam="Person.Location.address")]
    public ActionResult savePerson(Person p)
    {
        //
        return View();
    }