protected virtual IHtmlContent GenerateDisplay( ModelExplorer modelExplorer, string htmlFieldName, string templateName, object additionalViewData) { var modelEnum = modelExplorer.Model as Enum; if (modelExplorer.Metadata.IsEnum && modelEnum != null) { var value = modelEnum.ToString("d"); var enumGrouped = modelExplorer.Metadata.EnumGroupedDisplayNamesAndValues; Debug.Assert(enumGrouped != null); foreach (var kvp in enumGrouped) { if (kvp.Value == value) { // Creates a ModelExplorer with the same Metadata except that the Model is a string instead of an Enum modelExplorer = modelExplorer.GetExplorerForModel(kvp.Key.Name); break; } } } var templateBuilder = new TemplateBuilder( _viewEngine, _bufferScope, ViewContext, ViewData, modelExplorer, htmlFieldName, templateName, readOnly: true, additionalViewData: additionalViewData); return(templateBuilder.Build()); }
public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var model = viewData.Model; if (model == null) { return(HtmlString.Empty); } var enumerable = model as IEnumerable; if (enumerable == null) { // Only way we could reach here is if user passed templateName: "Collection" to an Editor() overload. throw new InvalidOperationException(Resources.FormatTemplates_TypeMustImplementIEnumerable( "Collection", model.GetType().FullName, typeof(IEnumerable).FullName)); } var elementMetadata = htmlHelper.ViewData.ModelMetadata.ElementMetadata; Debug.Assert(elementMetadata != null); var typeInCollectionIsNullableValueType = elementMetadata.IsNullableValueType; var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var metadataProvider = serviceProvider.GetRequiredService <IModelMetadataProvider>(); // Use typeof(string) instead of typeof(object) for IEnumerable collections. Neither type is Nullable<T>. if (elementMetadata.ModelType == typeof(object)) { elementMetadata = metadataProvider.GetMetadataForType(typeof(string)); } var oldPrefix = viewData.TemplateInfo.HtmlFieldPrefix; try { viewData.TemplateInfo.HtmlFieldPrefix = string.Empty; var collection = model as ICollection; var result = collection == null ? new HtmlContentBuilder() : new HtmlContentBuilder(collection.Count); var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var index = 0; foreach (var item in enumerable) { var itemMetadata = elementMetadata; if (item != null && !typeInCollectionIsNullableValueType) { itemMetadata = metadataProvider.GetMetadataForType(item.GetType()); } var modelExplorer = new ModelExplorer( metadataProvider, container: htmlHelper.ViewData.ModelExplorer, metadata: itemMetadata, model: item); var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", oldPrefix, index++); var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, modelExplorer, htmlFieldName: fieldName, templateName: null, readOnly: false, additionalViewData: null); result.AppendHtml(templateBuilder.Build()); } return(result); } finally { viewData.TemplateInfo.HtmlFieldPrefix = oldPrefix; } }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (templateInfo.TemplateDepth > 1) { if (modelExplorer.Model == null) { return(new HtmlString(modelExplorer.Metadata.NullDisplayText)); } var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { return(new StringHtmlContent(text)); } return(new HtmlString(text)); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count); foreach (var propertyExplorer in modelExplorer.PropertiesInternal) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: false, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); using (var writer = new HasContentTextWriter()) { label.WriteTo(writer, PassThroughHtmlEncoder.Default); if (writer.HasContent) { var labelTag = new TagBuilder("div"); labelTag.AddCssClass("editor-label"); labelTag.InnerHtml.SetHtmlContent(label); content.AppendLine(labelTag); } } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("editor-field"); valueDivTag.InnerHtml.AppendHtml(templateBuilderResult); valueDivTag.InnerHtml.AppendHtml(" "); valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage( propertyMetadata.PropertyName, message: null, htmlAttributes: null, tag: null)); content.AppendLine(valueDivTag); } else { content.AppendHtml(templateBuilderResult); } } return(content); }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (modelExplorer.Model == null) { return(new HtmlString(modelExplorer.Metadata.NullDisplayText)); } if (templateInfo.TemplateDepth > 1) { var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { text = htmlHelper.Encode(text); } return(new HtmlString(text)); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count); foreach (var propertyExplorer in modelExplorer.PropertiesInternal) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: true, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var label = propertyMetadata.GetDisplayName(); if (!string.IsNullOrEmpty(label)) { var labelTag = new TagBuilder("div"); labelTag.InnerHtml.SetContent(label); labelTag.AddCssClass("display-label"); content.AppendLine(labelTag); } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("display-field"); valueDivTag.InnerHtml.SetHtmlContent(templateBuilderResult); content.AppendLine(valueDivTag); } else { content.AppendHtml(templateBuilderResult); } } return(content); }