public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var model = viewData.Model; var result = new BufferedHtmlContent(); if (!viewData.ModelMetadata.HideSurroundingHtml) { result.Append(DefaultDisplayTemplates.StringTemplate(htmlHelper)); } // Special-case opaque values and arbitrary binary data. var modelAsByteArray = model as byte[]; if (modelAsByteArray != null) { model = Convert.ToBase64String(modelAsByteArray); } var htmlAttributesObject = viewData[HtmlAttributeKey]; var hiddenResult = htmlHelper.Hidden(expression: null, value: model, htmlAttributes: htmlAttributesObject); result.Append(hiddenResult); return(result); }
protected virtual IHtmlContent GenerateCheckBox( ModelExplorer modelExplorer, string expression, bool?isChecked, object htmlAttributes) { var checkbox = _htmlGenerator.GenerateCheckBox( ViewContext, modelExplorer, expression, isChecked, htmlAttributes); var hiddenForCheckboxTag = _htmlGenerator.GenerateHiddenForCheckbox(ViewContext, modelExplorer, expression); if (checkbox == null || hiddenForCheckboxTag == null) { return(HtmlString.Empty); } var checkboxContent = new BufferedHtmlContent().Append(checkbox); if (ViewContext.FormContext.CanRenderAtEndOfForm) { ViewContext.FormContext.EndOfFormContent.Add(hiddenForCheckboxTag); } else { checkboxContent.Append(hiddenForCheckboxTag); } return(checkboxContent); }
public void AppendString_AppendsAString() { // Arrange var content = new BufferedHtmlContent(); // Act content.Append("Hello"); // Assert var result = Assert.Single(content.Entries); Assert.IsType<string>(result); }
public void AppendString_WrittenAsEncoded() { // Arrange var content = new BufferedHtmlContent(); content.Append("Hello"); var writer = new StringWriter(); // Act content.WriteTo(writer, new CommonTestEncoder()); // Assert Assert.Equal("HtmlEncode[[Hello]]", writer.ToString()); }
public void AppendIHtmlContent_AppendsAsIs() { // Arrange var content = new BufferedHtmlContent(); var writer = new StringWriter(); // Act content.Append(new TestHtmlContent("Hello")); // Assert var result = Assert.Single(content.Entries); var testHtmlContent = Assert.IsType<TestHtmlContent>(result); testHtmlContent.WriteTo(writer, new CommonTestEncoder()); Assert.Equal("Written from TestHtmlContent: Hello", writer.ToString()); }
public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var model = viewData.Model; if (model == null) { return HtmlString.Empty; } var collection = model as IEnumerable; if (collection == 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 fieldNameBase = oldPrefix; var result = new BufferedHtmlContent(); var viewEngine = serviceProvider.GetRequiredService<ICompositeViewEngine>(); var index = 0; foreach (var item in collection) { 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}]", fieldNameBase, index++); var templateBuilder = new TemplateBuilder( viewEngine, htmlHelper.ViewContext, htmlHelper.ViewData, modelExplorer, htmlFieldName: fieldName, templateName: null, readOnly: false, additionalViewData: null); result.Append(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 content = new BufferedHtmlContent(); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, 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); if (!string.IsNullOrEmpty(label.ToString())) { var labelTag = new TagBuilder("div"); labelTag.AddCssClass("editor-label"); labelTag.InnerHtml.SetContent(label); content.AppendLine(labelTag); } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("editor-field"); valueDivTag.InnerHtml.Append(templateBuilderResult); valueDivTag.InnerHtml.AppendHtml(" "); valueDivTag.InnerHtml.Append(htmlHelper.ValidationMessage( propertyMetadata.PropertyName, message: null, htmlAttributes: null, tag: null)); content.AppendLine(valueDivTag); } else { content.Append(templateBuilderResult); } } return content; }
public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var model = viewData.Model; var result = new BufferedHtmlContent(); if (!viewData.ModelMetadata.HideSurroundingHtml) { result.Append(DefaultDisplayTemplates.StringTemplate(htmlHelper)); } // Special-case opaque values and arbitrary binary data. var modelAsByteArray = model as byte[]; if (modelAsByteArray != null) { model = Convert.ToBase64String(modelAsByteArray); } var htmlAttributesObject = viewData[HtmlAttributeKey]; var hiddenResult = htmlHelper.Hidden(expression: null, value: model, htmlAttributes: htmlAttributesObject); result.Append(hiddenResult); return result; }
public static IHtmlContent CollectionTemplate(IHtmlHelper htmlHelper) { var model = htmlHelper.ViewData.Model; if (model == null) { return(HtmlString.Empty); } var collection = model as IEnumerable; if (collection == null) { // Only way we could reach here is if user passed templateName: "Collection" to a Display() 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 = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix; try { htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty; var fieldNameBase = oldPrefix; var result = new BufferedHtmlContent(); var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var index = 0; foreach (var item in collection) { 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}]", fieldNameBase, index++); var templateBuilder = new TemplateBuilder( viewEngine, htmlHelper.ViewContext, htmlHelper.ViewData, modelExplorer, htmlFieldName: fieldName, templateName: null, readOnly: true, additionalViewData: null); result.Append(templateBuilder.Build()); } return(result); } finally { htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = oldPrefix; } }
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 content = new BufferedHtmlContent(); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, 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.SetContent(templateBuilderResult); content.AppendLine(valueDivTag); } else { content.Append(templateBuilderResult); } } return(content); }
public void CanAppendMultipleItems() { // Arrange var content = new BufferedHtmlContent(); // Act content.Append(new TestHtmlContent("hello")); content.Append("Test"); // Assert Assert.Equal(2, content.Entries.Count); Assert.Equal("Written from TestHtmlContent: hello", content.Entries[0].ToString()); Assert.Equal("Test", content.Entries[1]); }
public void WriteTo_WritesAllItems() { // Arrange var content = new BufferedHtmlContent(); var writer = new StringWriter(); content.Append(new TestHtmlContent("Hello")); content.Append("Test"); // Act content.WriteTo(writer, new CommonTestEncoder()); // Assert Assert.Equal(2, content.Entries.Count); Assert.Equal("Written from TestHtmlContent: HelloHtmlEncode[[Test]]", writer.ToString()); }
public void Clear_DeletesAllItems() { // Arrange var content = new BufferedHtmlContent(); content.Append(new TestHtmlContent("hello")); content.Append("Test"); // Act content.Clear(); // Assert Assert.Equal(0, content.Entries.Count); }