From 6c4462a76f5303da96c3854679ea3d6838f1192e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 22 Oct 2014 18:06:08 -0700 Subject: [PATCH] * Porting Hidden and HiddenFor unit tests Partial fix to #453 --- ...InputTest.cs => HtmlHelperCheckboxTest.cs} | 6 +- .../Rendering/HtmlHelperHiddenTest.cs | 768 ++++++++++++++++++ 2 files changed, 769 insertions(+), 5 deletions(-) rename test/Microsoft.AspNet.Mvc.Core.Test/Rendering/{HtmlHelperInputTest.cs => HtmlHelperCheckboxTest.cs} (99%) create mode 100644 test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperHiddenTest.cs diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperInputTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperCheckboxTest.cs similarity index 99% rename from test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperInputTest.cs rename to test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperCheckboxTest.cs index 7185c5e409..262a84d50d 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperInputTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperCheckboxTest.cs @@ -11,10 +11,8 @@ using Xunit; namespace Microsoft.AspNet.Mvc.Rendering { - public class HtmlHelperInputTest + public class HtmlHelperCheckboxTest { - // CheckBox - [Fact] public void CheckBoxOverridesCalculatedValuesWithValuesFromHtmlAttributes() { @@ -215,8 +213,6 @@ namespace Microsoft.AspNet.Mvc.Rendering Assert.Equal(expected, html.ToString()); } - //// CheckBoxFor - [Fact] public void CheckBoxForWithInvalidBooleanThrows() { diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperHiddenTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperHiddenTest.cs new file mode 100644 index 0000000000..1f4606aff5 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Rendering/HtmlHelperHiddenTest.cs @@ -0,0 +1,768 @@ +// Copyright (c) Microsoft Open Technologies, Inc. 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.Globalization; +using System.Linq.Expressions; +using Microsoft.AspNet.Mvc.ModelBinding; +using Microsoft.AspNet.Testing; +using Xunit; + +namespace Microsoft.AspNet.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, result.ToString()); + } + + [Theory] + [MemberData(nameof(HiddenWithAttributesData))] + public void HiddenWithArgumentValueAndAttributes_UsesArgumentValue(object attributes, string expected) + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = "should-not-be-used"; + + // Act + var result = helper.Hidden("Property1", "test", attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenOverridesValueFromAttributesWithArgumentValue() + { + // Arrange + var expected = @""; + var attributes = new { value = "attribute-value" }; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithNullModelAndNonNullViewData()); + helper.ViewData.Clear(); + + // Act + var result = helper.Hidden("Property1", "explicit-value", attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenWithArgumentValueAndNullModel_UsesArgumentValue() + { + // Arrange + var expected = @""; + var attributes = new { key = "value" }; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithNullModelAndNonNullViewData()); + + // Act + var result = helper.Hidden("Property1", "test", attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenWithNullValueAndNullModel_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var attributes = new Dictionary { { "data-key", "value" } }; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithNullModelAndNonNullViewData()); + + // Act + var result = helper.Hidden("Property1", "test", attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenUsesValuesFromModelState_OverExplicitSpecifiedValueAndPropertyValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(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, result.ToString()); + } + + [Fact] + public void HiddenUsesExplicitValue_IfModelStateDoesNotHaveProperty() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(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, result.ToString()); + } + + [Fact] + public void HiddenUsesValueFromViewData_IfModelStateDoesNotHavePropertyAndExplicitValueIsNull() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(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, result.ToString()); + } + + [Fact] + public void HiddenUsesPropertyValue_IfModelStateAndViewDataDoNotHavePropertyAndExplicitValueIsNull() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.ModelState.Clear(); + helper.ViewData.Clear(); + helper.ViewData.Model.Property1 = "property-value"; + + // Act + var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenDoesNotUsesAttributeValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.ModelState.Clear(); + helper.ViewData.Clear(); + helper.ViewData.Model.Property1 = null; + + // Act + var result = helper.Hidden("Property1", value: null, htmlAttributes: new { value = "attribute-value" }); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenReturnsEmptyValue_IfPropertyIsNotFound() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + var attributes = new Dictionary { { "baz", "BazValue" } }; + + // Act + var result = helper.Hidden("keyNotFound", value: null, htmlAttributes: attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenWithPrefix_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + + // Act + var result = helper.Hidden("Property1", "PropValue", htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenWithPrefixAndEmptyName_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + + // Act + var result = helper.Hidden(string.Empty, "fooValue", htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenUsesPrefixName_ToLookupPropertyValueInModelState() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + helper.IdAttributeDotReplacement = "$"; + helper.ViewData.ModelState.Clear(); + helper.ViewData.ModelState.Add("Property1", GetModelState("modelstate-without-prefix")); + helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelState("modelstate-with-prefix")); + helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelState("modelstate-with-iddotreplacement")); + + // Act + var result = helper.Hidden("Property1", "explicit-value", htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenUsesPrefixName_ToLookupPropertyValueInViewData() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + helper.IdAttributeDotReplacement = "$"; + 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, result.ToString()); + } + + [Fact] + public void HiddenWithEmptyNameAndPrefixThrows() + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + var attributes = new Dictionary + { + { "class", "some-class"} + }; + + // Act and Assert + ExceptionAssert.ThrowsArgumentNullOrEmpty(() => helper.Hidden(string.Empty, string.Empty, attributes), + "name"); + } + + [Fact] + public void HiddenWithViewDataErrors_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithErrors()); + var attributes = new Dictionary + { + { "baz", "BazValue" }, + { "class", "some-class"} + }; + + // Act + var result = helper.Hidden("Property1", value: null, htmlAttributes: attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenGeneratesUnobtrusiveValidation() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + + // Act + var result = helper.Hidden("Property2", value: null, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + 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.GetHtmlHelperForViewData(viewData); + var attributes = new Dictionary { { "data-test", "val" } }; + + // Act + var result = helper.Hidden(expression, value: null, htmlAttributes: attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + 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.GetHtmlHelperForViewData(viewData); + helper.IdAttributeDotReplacement = "$$"; + var attributes = new Dictionary { { "data-test", "val" } }; + + // Act + var result = helper.Hidden(expression, value: null, htmlAttributes: attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenForWithByteArrayValue_GeneratesBase64EncodedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Bytes = new byte[] { 23, 43, 53 }; + + // Act + var result = helper.HiddenFor(m => m.Bytes, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Theory] + [MemberData(nameof(HiddenWithAttributesData))] + public void HiddenForWithAttributes_GeneratesExpectedValue(object htmlAttributes, string expected) + { + // Arrange + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = "test"; + + // Act + var result = helper.HiddenFor(m => m.Property1, htmlAttributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenFor_UsesModelStateValueOverPropertyValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = "DefaultValue"; + + // Act + var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenFor_UsesPropertyValueIfModelStateDoesNotHaveKey() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.ModelState.Clear(); + helper.ViewData.Model.Property1 = "PropertyValue"; + + // Act + var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenForDoesNotUseValueFromViewDataDictionary_IfModelStateAndPropertyValueIsNull() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = null; + helper.ViewData.ModelState.Clear(); + + // Act + var result = helper.HiddenFor(m => m.Property1, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenForWithAttributesDictionaryAndNullModel_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithNullModelAndNonNullViewData()); + var attributes = new Dictionary { { "key", "value" } }; + + // Act + var result = helper.HiddenFor(m => m.Property1, attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + // This test ensures that specifying a the prefix does not affect the expression result. + [Fact] + public void HiddenForWithPrefix_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = "propValue"; + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + + // Act + var result = helper.HiddenFor(m => m.Property1); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenForWithPrefix_UsesPrefixWhenLookingUpModelStateValues() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithModelStateAndModelAndViewDataValues()); + helper.ViewData.Model.Property1 = "propValue"; + helper.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "MyPrefix"; + helper.IdAttributeDotReplacement = "$"; + helper.ViewData.ModelState.Clear(); + helper.ViewData.ModelState.Add("Property1", GetModelState("modelstate-without-prefix")); + helper.ViewData.ModelState.Add("MyPrefix.Property1", GetModelState("modelstate-with-prefix")); + helper.ViewData.ModelState.Add("MyPrefix$Property1", GetModelState("modelstate-with-iddotreplacement")); + + // Act + var result = helper.HiddenFor(m => m.Property1); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenForWithViewDataErrors_GeneratesExpectedValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithErrors()); + var attributes = new Dictionary + { + { "baz", "BazValue" }, + { "class", "some-class"} + }; + + // Act + var result = helper.HiddenFor(m => m.Property1, attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + [Fact] + public void HiddenFor_GeneratesUnobtrusiveValidationAttributes() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithErrors()); + + // Act + var result = helper.HiddenFor(m => m.Property2, htmlAttributes: null); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + 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.GetHtmlHelperForViewData(viewData); + var attributes = new Dictionary + { + { "data-val", "true" }, + { "value", "attr-val" } + }; + + // Act + var result = helper.HiddenFor(expression, attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + 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 HiddenFor_UsesModelStateValueForComplexExpressions( + Expression> expression, + string expected) + { + // Arrange + var viewData = GetViewDataWithNullModelAndNonNullViewData(); + viewData.ModelState.Add("pre.Property3[key]", GetModelState("Prop3Val")); + viewData.ModelState.Add("pre.Property4.Property5", GetModelState("Prop5Val")); + viewData.ModelState.Add("pre.Property4.Property6[0]", GetModelState("Prop6Val")); + + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(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, result.ToString()); + } + + [Fact] + public void HiddenFor_DoesNotUseAttributeValue() + { + // Arrange + var expected = @""; + var helper = DefaultTemplatesUtilities.GetHtmlHelperForViewData(GetViewDataWithNullModelAndNonNullViewData()); + var attributes = new Dictionary + { + { "value", "AttrValue" } + }; + + // Act + var result = helper.HiddenFor(m => m.Property1, attributes); + + // Assert + Assert.Equal(expected, result.ToString()); + } + + private static ViewDataDictionary GetViewDataWithNullModelAndNonNullViewData() + { + return new ViewDataDictionary(new EmptyModelMetadataProvider()) + { + ["Property1"] = "view-data-val", + }; + } + + private static ViewDataDictionary GetViewDataWithModelStateAndModelAndViewDataValues() + { + var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()) + { + Model = new HiddenModel(), + ["Property1"] = "view-data-val", + }; + viewData.ModelState.Add("Property1", GetModelState("ModelStateValue")); + + return viewData; + } + + private static ViewDataDictionary GetViewDataWithErrors() + { + var viewData = GetViewDataWithModelStateAndModelAndViewDataValues(); + viewData.ModelState.AddModelError("Property1", "error 1"); + viewData.ModelState.AddModelError("Property1", "error 2"); + return viewData; + } + + private static ModelState GetModelState(string value) + { + return new ModelState + { + Value = new ValueProviderResult(value, value, CultureInfo.InvariantCulture) + }; + } + + 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(); + } + } +} \ No newline at end of file