Beispiel #1
0
        public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model    = viewData.Model;

            var result = new HtmlContentBuilder();

            if (!viewData.ModelMetadata.HideSurroundingHtml)
            {
                result.AppendHtml(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.AppendHtml(hiddenResult);

            return(result);
        }
        public void ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne(
            string simpleDisplayText,
            bool htmlEncode,
            string expectedResult)
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel();

            model.Property1 = simpleDisplayText;

            var provider = new TestModelMetadataProvider();

            provider.ForType <DefaultTemplatesUtilities.ObjectTemplateModel>().DisplayDetails(dd =>
            {
                dd.HtmlEncode            = htmlEncode;
                dd.SimpleDisplayProperty = "Property1";
            });

            var html = DefaultTemplatesUtilities.GetHtmlHelper(model, provider);

            html.ViewData.TemplateInfo.AddVisited("foo");
            html.ViewData.TemplateInfo.AddVisited("bar");

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expectedResult, HtmlContentUtilities.HtmlContentToString(result));
        }
Beispiel #3
0
        public static IHtmlContent HiddenInputTemplate(IHtmlHelper htmlHelper)
        {
            var viewData = htmlHelper.ViewData;
            var model    = viewData.Model;

            IHtmlContent display;

            if (viewData.ModelMetadata.HideSurroundingHtml)
            {
                display = null;
            }
            else
            {
                display = DefaultDisplayTemplates.StringTemplate(htmlHelper);
            }

            var htmlAttributesObject = viewData[HtmlAttributeKey];
            var hidden = htmlHelper.Hidden(expression: null, value: model, htmlAttributes: htmlAttributesObject);

            if (viewData.ModelMetadata.HideSurroundingHtml)
            {
                return(hidden);
            }

            return(new HtmlContentBuilder(capacity: 2)
                   .AppendHtml(display)
                   .AppendHtml(hidden));
        }
        public void HiddenInputTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var model = "Model string";

            var provider = new TestModelMetadataProvider();

            provider.ForType <string>().DisplayDetails(dd =>
            {
                dd.HideSurroundingHtml = true;
            });

            var html     = DefaultTemplatesUtilities.GetHtmlHelper(model, provider: provider);
            var viewData = html.ViewData;

            var templateInfo = viewData.TemplateInfo;

            templateInfo.HtmlFieldPrefix     = "FieldPrefix";
            templateInfo.FormattedModelValue = "Formatted string";

            // Act
            var result = DefaultDisplayTemplates.HiddenInputTemplate(html);

            // Assert
            Assert.Empty(HtmlContentUtilities.HtmlContentToString(result));
        }
        public void ObjectTemplate_HonoursHideSurroundingHtml()
        {
            // Arrange
            var expected =
                "Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
                "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Prop2]]</div>" + Environment.NewLine +
                "<div class=\"HtmlEncode[[display-field]]\">Model = (null), ModelType = System.String, PropertyName = Property2," +
                " SimpleDisplayText = (null)</div>" + Environment.NewLine;

            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "p1", Property2 = null
            };

            var provider = new TestModelMetadataProvider();

            provider.ForProperty <DefaultTemplatesUtilities.ObjectTemplateModel>("Property1").DisplayDetails(dd =>
            {
                dd.HideSurroundingHtml = true;
            });

            var html = DefaultTemplatesUtilities.GetHtmlHelper(model, provider);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
        public void ObjectTemplate_IgnoresPropertiesWith_ScaffoldColumnFalse()
        {
            // Arrange
            var expected = "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property1]]</div>" + Environment.NewLine +
                           "<div class=\"HtmlEncode[[display-field]]\"></div>" + Environment.NewLine +
                           "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property3]]</div>" + Environment.NewLine +
                           "<div class=\"HtmlEncode[[display-field]]\"></div>" + Environment.NewLine;

            var model      = new DefaultTemplatesUtilities.ObjectWithScaffoldColumn();
            var viewEngine = new Mock <ICompositeViewEngine>(MockBehavior.Strict);

            viewEngine
            .Setup(v => v.GetView(/*executingFilePath*/ null, It.IsAny <string>(), /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(string.Empty, Enumerable.Empty <string>()));
            viewEngine
            .Setup(v => v.FindView(It.IsAny <ActionContext>(), It.IsAny <string>(), /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(string.Empty, Enumerable.Empty <string>()));
            var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(model, viewEngine.Object);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(htmlHelper);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
Beispiel #7
0
 private static IHtmlContent BooleanTemplateDropDownList(IHtmlHelper htmlHelper, bool?value)
 {
     return(htmlHelper.DropDownList(
                expression: null,
                selectList: DefaultDisplayTemplates.TriStateValues(value),
                optionLabel: null,
                htmlAttributes: CreateHtmlAttributes(htmlHelper, "list-box tri-state")));
 }
        public void ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull()
        {
            // Arrange
            var provider = new TestModelMetadataProvider();

            provider.ForType <DefaultTemplatesUtilities.ObjectTemplateModel>().DisplayDetails(dd =>
            {
                dd.NullDisplayText = "(null value)";
            });

            var html = DefaultTemplatesUtilities.GetHtmlHelper(provider: provider);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal("(null value)", HtmlContentUtilities.HtmlContentToString(result));
        }
        public void HiddenInputTemplate_ReturnsValue()
        {
            // Arrange
            var model        = "Model string";
            var html         = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var templateInfo = html.ViewData.TemplateInfo;

            templateInfo.HtmlFieldPrefix = "FieldPrefix";

            // TemplateBuilder sets FormattedModelValue before calling TemplateRenderer and it's used below.
            templateInfo.FormattedModelValue = "Formatted string";

            // Act
            var result = DefaultDisplayTemplates.HiddenInputTemplate(html);

            // Assert
            Assert.Equal("HtmlEncode[[Formatted string]]", HtmlContentUtilities.HtmlContentToString(result));
        }
        public void ObjectTemplate_OrdersProperties_AsExpected()
        {
            // Arrange
            var model = new OrderedModel();
            var html  = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var expectedProperties = new List <string>
            {
                "OrderedProperty3",
                "OrderedProperty2",
                "OrderedProperty1",
                "Property3",
                "Property1",
                "Property2",
                "LastProperty",
            };

            var stringBuilder = new StringBuilder();

            foreach (var property in expectedProperties)
            {
                var label = string.Format(
                    CultureInfo.InvariantCulture,
                    "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[{0}]]</div>",
                    property);
                stringBuilder.AppendLine(label);

                var value = string.Format(
                    CultureInfo.InvariantCulture,
                    "<div class=\"HtmlEncode[[display-field]]\">Model = (null), ModelType = System.String, PropertyName = {0}, " +
                    "SimpleDisplayText = (null)</div>",
                    property);
                stringBuilder.AppendLine(value);
            }
            var expected = stringBuilder.ToString();

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }
        public void ObjectTemplateDisplaysSimplePropertiesOnObjectByDefault()
        {
            var expected =
                "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property1]]</div>" + Environment.NewLine
                + "<div class=\"HtmlEncode[[display-field]]\">Model = p1, ModelType = System.String, PropertyName = Property1," +
                " SimpleDisplayText = p1</div>" + Environment.NewLine
                + "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Prop2]]</div>" + Environment.NewLine
                + "<div class=\"HtmlEncode[[display-field]]\">Model = (null), ModelType = System.String, PropertyName = Property2," +
                " SimpleDisplayText = (null)</div>" + Environment.NewLine;

            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel {
                Property1 = "p1", Property2 = null
            };
            var html = DefaultTemplatesUtilities.GetHtmlHelper(model);

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
        }