public void Render(HtmlHelper helper, PartialRequest partialRequestHandler, IContentData contentData, TemplateModel templateModel) { var output = _htmlCache.GetOrAdd(CreateCacheKey(contentData, templateModel), context => { AddDependecies(context, contentData); var buffer = new StringBuilder(); using (var writer = new StringWriter(buffer)) { var orgWriter = helper.ViewContext.Writer; try { helper.ViewContext.Writer = writer; _defaultRenderer.Render(helper, partialRequestHandler, contentData, templateModel); } finally { helper.ViewContext.Writer = orgWriter; } } return(buffer.ToString()); }); helper.ViewContext.Writer.Write(output); }
public override MvcHtmlString PropertyFor <TModel, TValue>(HtmlHelper <TModel> html, string viewModelPropertyName, object additionalViewData, object editorSettings, Expression <Func <TModel, TValue> > expression, Func <string, MvcHtmlString> displayForAction) { Func <MvcHtmlString> defaultRendering = () => _defaultRenderer.PropertyFor(html, viewModelPropertyName, additionalViewData, editorSettings, expression, displayForAction); //We do not want cache in edit mode if (_contextModeResolver.CurrentMode.EditOrPreview()) { return(defaultRendering()); } var additionalValuesDictionary = new RouteValueDictionary(additionalViewData); var contentDependecies = GetContentDependecies <TModel>(html, additionalValuesDictionary); //We need to have a cachekey that is unique for the context of this property rendering. We use propertyname, content items, and template to build up the key return(contentDependecies.Any() ? new MvcHtmlString(_htmlCache.GetOrAdd(CreateCacheKey(contentDependecies, viewModelPropertyName, ResolveTemplateName(expression, html, additionalValuesDictionary)), c => { c.AddDependencies(contentDependecies); return defaultRendering().ToString(); })) : defaultRendering()); }