c# - Generate one MVC model to be used in multiple views -
i'm new concepts of mvc , asp.net , i'm wondering if there possibility create object of model once , use throughout different views.
i'm writing application gets json object through web service call contains information populate. web service call needs id create right json object. since json object quite large web service call takes 2 seconds download json object. when switching views model generated (including download of json object) every time, adds enormous overhead.
generating different view models different views not work, since download bottleneck.
any ideas how tackle problem? can downloaded json string stored somehow used in different views? possible download json object if id changes?
regards
something along these lines work:
public actionresult myview(int someid) { string json = this.getjson(someid); var model = new myviewmodel(json); return this.view(model); } private string getjson(int id) { string cachekey = "myjsoncachekey" + id; string cachedjson = this.httpcontext.cache[cachekey] string; if (cachedjson != null) return cachedjson; string actualjson = new webclient().downloadstring("http://whatever"); this.httpcontext.cache.insert(cachekey, actualjson); return actualjson; }
just 2 points note:
- remember implement appropriate thread safety mechanism
- you can use other caching provider instead of
httpcontext.cache
, code pretty similar
Comments
Post a Comment