Esempio n. 1
0
        internal static string ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, string templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames)
        {
            Dictionary <string, ActionCacheItem>            actionCache    = GetActionCache(html);
            Dictionary <string, Func <HtmlHelper, string> > defaultActions = mode == DataBoundControlMode.ReadOnly ? defaultDisplayActions : defaultEditorActions;
            string modeViewPath = modeViewPaths[mode];

            foreach (string viewName in getViewNames(viewData.ModelMetadata, templateName, viewData.ModelMetadata.TemplateHint, viewData.ModelMetadata.DataTypeName))
            {
                string          fullViewName = modeViewPath + "/" + viewName;
                ActionCacheItem cacheItem;

                if (actionCache.TryGetValue(fullViewName, out cacheItem))
                {
                    if (cacheItem != null)
                    {
                        return(cacheItem.Execute(html, viewData));
                    }
                }
                else
                {
                    ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(html.ViewContext, fullViewName);
                    if (viewEngineResult.View != null)
                    {
                        actionCache[fullViewName] = new ActionCacheViewItem {
                            ViewName = fullViewName
                        };

                        StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
                        viewEngineResult.View.Render(new ViewContext(html.ViewContext, viewEngineResult.View, viewData, html.ViewContext.TempData, writer), writer);
                        return(writer.ToString());
                    }

                    Func <HtmlHelper, string> defaultAction;
                    if (defaultActions.TryGetValue(viewName, out defaultAction))
                    {
                        actionCache[fullViewName] = new ActionCacheCodeItem {
                            Action = defaultAction
                        };
                        return(defaultAction(
                                   new HtmlHelper(
                                       new ViewContext(html.ViewContext, html.ViewContext.View, viewData, html.ViewContext.TempData, html.ViewContext.Writer),
                                       html.ViewDataContainer
                                       )
                                   ));
                    }

                    actionCache[fullViewName] = null;
                }
            }

            throw new InvalidOperationException(
                      String.Format(
                          CultureInfo.CurrentCulture,
                          MvcResources.TemplateHelpers_NoTemplate,
                          viewData.ModelMetadata.RealModelType.FullName
                          )
                      );
        }
Esempio n. 2
0
        internal static string ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, string templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
        {
            var actionCache = GetActionCache(html);
            var dictionary  = getDefaultActions(mode);
            var str         = _modeViewPaths[mode];

            foreach (var key in getViewNames(viewData.ModelMetadata, new string[3]
            {
                templateName,
                viewData.ModelMetadata.TemplateHint,
                viewData.ModelMetadata.DataTypeName
            }))
            {
                var             index = str + "/" + key;
                ActionCacheItem actionCacheItem;
                if (actionCache.TryGetValue(index, out actionCacheItem))
                {
                    if (actionCacheItem != null)
                    {
                        return(actionCacheItem.Execute(html, viewData));
                    }
                }
                else
                {
                    // we must suppress global templates, because those from other areas may not know Angular
                    // so, first we look for angular attribute, but we let them pass if an UIHint was attached
                    var isAngular = false;
                    if (viewData.ModelMetadata.ContainerType != null)
                    {
                        if (viewData.ModelMetadata.PropertyName != null)
                        {
                            isAngular = viewData.ModelMetadata.ContainerType.GetProperty(viewData.ModelMetadata.PropertyName).GetCustomAttributes(true).OfType <NgFieldAttribute>().Any();
                        }
                    }
                    if (!isAngular)
                    {
                        var partialView = ViewEngines.Engines.FindPartialView(html.ViewContext, index);
                        if (partialView.View != null)
                        {
                            actionCache[index] = new ActionCacheViewItem()
                            {
                                ViewName = index
                            };
                            using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture)) {
                                partialView.View.Render(new ViewContext(html.ViewContext, partialView.View, viewData, html.ViewContext.TempData, stringWriter), stringWriter);
                                return(stringWriter.ToString());
                            }
                        }
                    }
                    Func <HtmlHelper, string> func;
                    if (dictionary.TryGetValue(key, out func))
                    {
                        actionCache[index] = new ActionCacheCodeItem()
                        {
                            Action = func
                        };
                        return(func(MakeHtmlHelper(html, viewData)));
                    }
                    actionCache[index] = null;
                }
            }
            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "TemplateHelpers_NoTemplate", new object[1]
            {
                viewData.ModelMetadata.ModelType.FullName
            }));
        }