// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq.Expressions; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.TestCommon; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Testing; using Xunit; namespace Microsoft.AspNetCore.Mvc.Rendering { public class HtmlHelperHiddenTest { public static IEnumerable HiddenWithAttributesData { get { var expected1 = @""; yield return new object[] { new Dictionary { { "baz", "BazValue" } }, expected1 }; yield return new object[] { new { baz = "BazValue" }, expected1 }; var expected2 = @""; yield return new object[] { new Dictionary { { "foo-baz", "BazValue" } }, expected2 }; yield return new object[] { new { foo_baz = "BazValue" }, expected2 }; } } [Fact] public void HiddenWithByteArrayValue_GeneratesBase64EncodedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(); // Act var result = helper.Hidden("ProductName", new byte[] { 23, 43, 53 }, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Theory] [MemberData(nameof(HiddenWithAttributesData))] public void HiddenWithArgumentValueAndAttributes_UsesArgumentValue(object attributes, string expected) { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = "should-not-be-used"; // Act var result = helper.Hidden("Property1", "test", attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenNotInTemplate_GetsValueFromPropertyOfViewDataEntry() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.Model.Property1 = "model-property1-value"; helper.ViewData["Prefix"] = new HiddenModel { Property1 = "contained-view-data-value" }; // Act var html = helper.Hidden("Prefix.Property1", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenInTemplate_GetsValueFromPropertyOfViewDataEntry() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; helper.ViewData.Model.Property1 = "model-property1-value"; helper.ViewData["Prefix"] = new HiddenModel { Property1 = "contained-view-data-value" }; // Act var html = helper.Hidden("Property1", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenNotInTemplate_GetsValueFromViewDataEntry_EvenIfNull() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.Model.Property1 = "model-property1-value"; helper.ViewData["Property1"] = null; // Act var html = helper.Hidden("Property1", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenInTemplate_GetsValueFromViewDataEntry_EvenIfNull() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; helper.ViewData.Model.Property1 = "model-property1-value"; helper.ViewData["Prefix.Property1"] = null; // Act var html = helper.Hidden("Property1", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenOverridesValueFromAttributesWithArgumentValue() { // Arrange var expected = @""; var attributes = new { value = "attribute-value" }; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNullModelAndNonNullViewData()); helper.ViewData.Clear(); // Act var result = helper.Hidden("Property1", "explicit-value", attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenWithArgumentValueAndNullModel_UsesArgumentValue() { // Arrange var expected = @""; var attributes = new { key = "value" }; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNullModelAndNonNullViewData()); // Act var result = helper.Hidden("Property1", "test", attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenWithNonNullValue_GeneratesExpectedValue() { // Arrange var expected = @""; var attributes = new Dictionary { { "data-key", "value" } }; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNullModelAndNonNullViewData()); // Act var result = helper.Hidden("Property1", "test", attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenUsesValuesFromModelState_OverExplicitSpecifiedValueAndPropertyValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = "test-value"; // Act var result = helper.Hidden("Property1", value: "explicit-value", htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenUsesExplicitValue_IfModelStateDoesNotHaveProperty() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.ModelState.Clear(); helper.ViewData.Model.Property1 = "property-value"; // Act var result = helper.Hidden("Property1", value: "explicit-value", htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenUsesValueFromViewData_IfModelStateDoesNotHavePropertyAndExplicitValueIsNull() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.ModelState.Clear(); helper.ViewData.Model.Property1 = "property-value"; // Act var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenNotInTemplate_GetsModelValue_IfModelStateAndViewDataEmpty() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.Model.Property1 = "property-value"; // Act var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_GetsModelValue_IfModelStateAndViewDataEmpty() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; helper.ViewData.Model.Property1 = "property-value"; // Act var html = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenNotInTemplate_DoesNotUseAttributeValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); // Act var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_DoesNotUseAttributeValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNonNullModel()); helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; // Act var html = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenNotInTemplate_GetsEmptyValue_IfPropertyIsNotFound() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); var attributes = new Dictionary { { "baz", "BazValue" } }; // Act var result = helper.Hidden("keyNotFound", value: null, htmlAttributes: attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_GetsEmptyValue_IfPropertyIsNotFound() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.TemplateInfo.HtmlFieldPrefix = "Prefix"; // Act var html = helper.Hidden("keyNotFound", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(html)); } [Fact] public void HiddenInTemplate_WithExplicitValue_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act var result = helper.Hidden("Property1", "PropValue", htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_WithExplicitValueAndEmptyName_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act var result = helper.Hidden(string.Empty, "fooValue", htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_UsesPrefixName_ToLookupPropertyValueInModelState() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper( GetViewDataWithModelStateAndModelAndViewDataValues(), idAttributeDotReplacement: "$"); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; helper.ViewData.ModelState.Clear(); helper.ViewData.ModelState.SetModelValue( "Property1", "modelstate-without-prefix", "modelstate-without-prefix"); helper.ViewData.ModelState.SetModelValue( "MyPrefix.Property1", "modelstate-with-prefix", "modelstate-with-prefix"); helper.ViewData.ModelState.SetModelValue( "MyPrefix$Property1", "modelstate-with-iddotreplacement", "modelstate-with-iddotreplacement"); // Act var result = helper.Hidden("Property1", "explicit-value", htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenInTemplate_UsesPrefixNameToLookupPropertyValueInViewData() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper( GetViewDataWithModelStateAndModelAndViewDataValues(), idAttributeDotReplacement: "$"); helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; helper.ViewData.ModelState.Clear(); helper.ViewData.Clear(); helper.ViewData.Add("Property1", "vdd-without-prefix"); helper.ViewData.Add("MyPrefix.Property1", "vdd-with-prefix"); helper.ViewData.Add("MyPrefix$Property1", "vdd-with-iddotreplacement"); // Act var result = helper.Hidden("Property1", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenWithEmptyNameAndPrefixThrows() { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); var attributes = new Dictionary { { "class", "some-class"} }; var expected = "The name of an HTML field cannot be null or empty. Instead use methods " + "Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper.Editor or Microsoft.AspNetCore.Mvc.Rendering." + "IHtmlHelper`1.EditorFor with a non-empty htmlFieldName argument value."; // Act and Assert ExceptionAssert.ThrowsArgument( () => helper.Hidden(string.Empty, string.Empty, attributes), "expression", expected); } [Fact] public void HiddenWithViewDataErrors_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithErrors()); var attributes = new Dictionary { { "baz", "BazValue" }, { "class", "some-class"} }; // Act var result = helper.Hidden("Property1", value: null, htmlAttributes: attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenGeneratesUnobtrusiveValidation() { // Arrange var requiredMessage = new RequiredAttribute().FormatErrorMessage("Property2"); var expected = $@""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); // Act var result = helper.Hidden("Property2", value: null, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } public static IEnumerable HiddenWithComplexExpressions_UsesValueFromViewDataData { get { yield return new object[] { "Property3[height]", @"", }; yield return new object[] { "Property4.Property5", @"", }; yield return new object[] { "Property4.Property6[0]", @"", }; } } [Theory] [MemberData(nameof(HiddenWithComplexExpressions_UsesValueFromViewDataData))] public void HiddenWithComplexExpressions_UsesValueFromViewData(string expression, string expected) { // Arrange var viewData = GetViewDataWithModelStateAndModelAndViewDataValues(); viewData["Property3[height]"] = "Prop3Value"; viewData["Property4.Property5"] = "Prop5Value"; viewData["Property4.Property6[0]"] = "Prop6Value"; var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData); var attributes = new Dictionary { { "data-test", "val" } }; // Act var result = helper.Hidden(expression, value: null, htmlAttributes: attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } public static IEnumerable HiddenWithComplexExpressions_UsesIdDotSeparatorData { get { yield return new object[] { "Property4.Property5", @"", }; yield return new object[] { "Property4.Property6[0]", @"", }; } } [Theory] [MemberData(nameof(HiddenWithComplexExpressions_UsesIdDotSeparatorData))] public void HiddenWithComplexExpressions_UsesIdDotSeparator(string expression, string expected) { // Arrange var viewData = GetViewDataWithModelStateAndModelAndViewDataValues(); viewData["Property3[height]"] = "Prop3Value"; viewData["Property4.Property5"] = "Prop5Value"; viewData["Property4.Property6[0]"] = "Prop6Value"; var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData, idAttributeDotReplacement: "$$"); var attributes = new Dictionary { { "data-test", "val" } }; // Act var result = helper.Hidden(expression, value: null, htmlAttributes: attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenForWithByteArrayValue_GeneratesBase64EncodedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Bytes = new byte[] { 23, 43, 53 }; // Act var result = helper.HiddenFor(m => m.Bytes, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Theory] [MemberData(nameof(HiddenWithAttributesData))] public void HiddenForWithAttributes_GeneratesExpectedValue(object htmlAttributes, string expected) { // Arrange var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = "test"; // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenFor_UsesModelStateValueOverPropertyValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = "DefaultValue"; // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenFor_UsesPropertyValueIfModelStateDoesNotHaveKey() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.ModelState.Clear(); helper.ViewData.Model.Property1 = "PropertyValue"; // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenForDoesNotUseValueFromViewDataDictionary_IfModelStateAndPropertyValueIsNull() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = null; helper.ViewData.ModelState.Clear(); // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenForWithAttributesDictionaryAndNullModel_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNullModelAndNonNullViewData()); var attributes = new Dictionary { { "key", "value" } }; // Act var result = helper.HiddenFor(m => m.Property1, attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } // This test ensures that specifying a the prefix does not affect the expression result. [Fact] public void HiddenForInTemplate_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithModelStateAndModelAndViewDataValues()); helper.ViewData.Model.Property1 = "propValue"; helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenForInTemplate_UsesPrefixWhenLookingUpModelStateValues() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper( GetViewDataWithModelStateAndModelAndViewDataValues(), "$"); helper.ViewData.Model.Property1 = "propValue"; helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; helper.ViewData.ModelState.Clear(); helper.ViewData.ModelState.SetModelValue( "Property1", "modelstate-without-prefix", "modelstate-without-prefix"); helper.ViewData.ModelState.SetModelValue( "MyPrefix.Property1", "modelstate-with-prefix", "modelstate-with-prefix"); helper.ViewData.ModelState.SetModelValue( "MyPrefix$Property1", "modelstate-with-iddotreplacement", "modelstate-with-iddotreplacement"); // Act var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenForWithViewDataErrors_GeneratesExpectedValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithErrors()); var attributes = new Dictionary { { "baz", "BazValue" }, { "class", "some-class"} }; // Act var result = helper.HiddenFor(m => m.Property1, attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenFor_GeneratesUnobtrusiveValidationAttributes() { // Arrange var requiredMessage = ValidationAttributeUtil.GetRequiredErrorMessage("Property2"); var expected = $@""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithErrors()); // Act var result = helper.HiddenFor(m => m.Property2, htmlAttributes: null); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } public static TheoryData HiddenFor_UsesPropertyValueIfModelStateDoesNotContainValueData { get { var localModel = new HiddenModel(); localModel.Property4.Property5 = "local-value"; return new TheoryData>, string> { { model => model.Property3["key"], @"" }, { model => model.Property4.Property5, @"" }, { model => model.Property4.Property6[0], @"" }, { model => localModel.Property4.Property5, @"" } }; } } [Theory] [MemberData(nameof(HiddenFor_UsesPropertyValueIfModelStateDoesNotContainValueData))] public void HiddenFor_UsesPropertyValueIfModelStateDoesNotContainValue( Expression> expression, string expected) { // Arrange var viewData = GetViewDataWithModelStateAndModelAndViewDataValues(); viewData["Property3[key]"] = "Prop3Val"; viewData["Property4.Property5"] = "Prop4Val"; viewData["Property4.Property6[0]"] = "Prop6Val"; viewData.Model.Property3["key"] = "ModelProp3Val"; viewData.Model.Property4.Property5 = "ModelProp5Val"; viewData.Model.Property4.Property6.Add("ModelProp6Val"); var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData); var attributes = new Dictionary { { "data-val", "true" }, { "value", "attr-val" } }; // Act var result = helper.HiddenFor(expression, attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } public static TheoryData HiddenFor_UsesModelStateValueForComplexExpressionsData { get { return new TheoryData>, string> { { model => model.Property3["key"], @"" }, { model => model.Property4.Property5, @"" }, { model => model.Property4.Property6[0], @"" } }; } } [Theory] [MemberData(nameof(HiddenFor_UsesModelStateValueForComplexExpressionsData))] public void HiddenForInTemplate_UsesModelStateValueForComplexExpressions( Expression> expression, string expected) { // Arrange var viewData = GetViewDataWithNullModelAndNonNullViewData(); viewData.ModelState.SetModelValue("pre.Property3[key]", "Prop3Val", "Prop3Val"); viewData.ModelState.SetModelValue("pre.Property4.Property5", "Prop5Val", "Prop5Val"); viewData.ModelState.SetModelValue("pre.Property4.Property6[0]", "Prop6Val", "Prop6Val"); var helper = DefaultTemplatesUtilities.GetHtmlHelper(viewData); viewData.TemplateInfo.HtmlFieldPrefix = "pre"; var attributes = new { data_val = "true", value = "attr-val" }; // Act var result = helper.HiddenFor(expression, attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void HiddenFor_DoesNotUseAttributeValue() { // Arrange var expected = @""; var helper = DefaultTemplatesUtilities.GetHtmlHelper(GetViewDataWithNullModelAndNonNullViewData()); var attributes = new Dictionary { { "value", "AttrValue" } }; // Act var result = helper.HiddenFor(m => m.Property1, attributes); // Assert Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result)); } [Fact] public void Hidden_UsesSpecifiedExpression() { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property1 = "propValue" }; // Act var hiddenResult = helper.Hidden("Property1"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(hiddenResult)); } [Fact] public void HiddenFor_UsesSpecifiedExpression() { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property1 = "propValue" }; // Act var hiddenForResult = helper.HiddenFor(m => m.Property1); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(hiddenForResult)); } [Fact] public void Hidden_UsesSpecifiedValue() { // Arrange var metadataProvider = new EmptyModelMetadataProvider(); var helper = DefaultTemplatesUtilities.GetHtmlHelper(new ViewDataDictionary(metadataProvider)); helper.ViewContext.ClientValidationEnabled = false; helper.ViewData.Model = new TestModel { Property1 = "propValue" }; // Act var hiddenResult = helper.Hidden("Property1", value: "myvalue"); // Assert Assert.Equal( "", HtmlContentUtilities.HtmlContentToString(hiddenResult)); } private static ViewDataDictionary GetViewDataWithNullModelAndNonNullViewData() { return new ViewDataDictionary(new EmptyModelMetadataProvider()) { ["Property1"] = "view-data-val", }; } private static ViewDataDictionary GetViewDataWithNonNullModel() { var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()) { Model = new HiddenModel(), }; return viewData; } private static ViewDataDictionary GetViewDataWithModelStateAndModelAndViewDataValues() { var viewData = GetViewDataWithNonNullModel(); viewData["Property1"] = "view-data-val"; viewData.ModelState.SetModelValue("Property1", "ModelStateValue", "ModelStateValue"); return viewData; } private static ViewDataDictionary GetViewDataWithErrors() { var viewData = GetViewDataWithModelStateAndModelAndViewDataValues(); viewData.ModelState.AddModelError("Property1", "error 1"); viewData.ModelState.AddModelError("Property1", "error 2"); return viewData; } public class HiddenModel { public string Property1 { get; set; } public byte[] Bytes { get; set; } [Required] public string Property2 { get; set; } public Dictionary Property3 { get; } = new Dictionary(); public NestedClass Property4 { get; } = new NestedClass(); } public class NestedClass { public string Property5 { get; set; } public List Property6 { get; } = new List(); } private class TestModel { public string Property1 { get; set; } } } }