当前位置: 首页>>代码示例>>C#>>正文


C# FormContext类代码示例

本文整理汇总了C#中FormContext的典型用法代码示例。如果您正苦于以下问题:C# FormContext类的具体用法?C# FormContext怎么用?C# FormContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


FormContext类属于命名空间,在下文中一共展示了FormContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BeginFormWithClientValidationEnabled

        public void BeginFormWithClientValidationEnabled() {
            // Arrange
            StringWriter writer;
            HtmlHelper htmlHelper = GetFormHelper(out writer);

            FormContext originalContext = new FormContext();
            htmlHelper.ViewContext.ClientValidationEnabled = true;
            htmlHelper.ViewContext.FormContext = originalContext;

            // Act & assert - push
            MvcForm theForm = htmlHelper.BeginForm();
            Assert.IsNotNull(htmlHelper.ViewContext.FormContext);
            Assert.AreNotEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext should have been set to a new instance.");
            Assert.AreEqual("form_id", htmlHelper.ViewContext.FormContext.FormId);

            // Act & assert - pop
            theForm.Dispose();
            Assert.AreEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext was not properly restored.");
            Assert.AreEqual(@"<form action=""/some/path"" id=""form_id"" method=""post""></form><script type=""text/javascript"">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({""Fields"":[],""FormId"":""form_id"",""ReplaceValidationSummary"":false});
//]]>
</script>", writer.ToString());
        }
开发者ID:jesshaw,项目名称:ASP.NET-Mvc-3,代码行数:25,代码来源:FormExtensionsTest.cs

示例2: BeginFormWithClientValidationEnabled

        public void BeginFormWithClientValidationEnabled() {
            // Arrange
            Mock<HttpResponseBase> mockHttpResponse;
            HtmlHelper htmlHelper = GetFormHelper(@"<form action=""/some/path"" id=""form_id"" method=""post"">", @"</form>", out mockHttpResponse);
            mockHttpResponse.Expect(r => r.Write(@"<script type=""text/javascript"">
//<![CDATA[
EnableClientValidation({""Fields"":[],""FormId"":""form_id""}, null);
//]]>
</script>")).Verifiable();

            FormContext originalContext = new FormContext();
            htmlHelper.ClientValidationEnabled = true;
            htmlHelper.ViewContext.FormContext = originalContext;

            // Act & assert - push
            MvcForm theForm = htmlHelper.BeginForm();
            Assert.IsNotNull(htmlHelper.ViewContext.FormContext);
            Assert.AreNotEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext should have been set to a new instance.");
            Assert.AreEqual("form_id", htmlHelper.ViewContext.FormContext.FormId);
            Assert.IsTrue(htmlHelper.ViewContext.FormContext.ClientValidationEnabled);

            // Act & assert - pop
            theForm.Dispose();
            Assert.AreEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext was not properly restored.");
            mockHttpResponse.Verify();
        }
开发者ID:Marceli,项目名称:JQueryGridTest,代码行数:26,代码来源:FormExtensionsTest.cs

示例3: ReplaceValidationSummaryProperty

        public void ReplaceValidationSummaryProperty()
        {
            // Arrange
            FormContext context = new FormContext();

            // Act & Assert
            MemberHelper.TestBooleanProperty(context, "ReplaceValidationSummary", false, false);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:8,代码来源:FormContextTest.cs

示例4: MvcForm

        public MvcForm(ViewContext viewContext) {
            if (viewContext == null) {
                throw new ArgumentNullException("viewContext");
            }

            _viewContext = viewContext;
            _writer = viewContext.Writer;

            // push the new FormContext
            _originalFormContext = viewContext.FormContext;
            viewContext.FormContext = new FormContext();
        }
开发者ID:nobled,项目名称:mono,代码行数:12,代码来源:MvcForm.cs

示例5: FieldValidatorsProperty

        public void FieldValidatorsProperty()
        {
            // Arrange
            FormContext context = new FormContext();

            // Act
            IDictionary<String, FieldValidationMetadata> fieldValidators = context.FieldValidators;

            // Assert
            Assert.NotNull(fieldValidators);
            Assert.Empty(fieldValidators);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:12,代码来源:FormContextTest.cs

示例6: MvcForm

        internal MvcForm(ViewContext viewContext) {
            if (viewContext == null) {
                throw new ArgumentNullException("viewContext");
            }

            _viewContext = viewContext;
            _httpResponse = viewContext.HttpContext.Response;

            // push the new FormContext
            _originalFormContext = viewContext.FormContext;
            viewContext.FormContext = new FormContext();
        }
开发者ID:Marceli,项目名称:JQueryGridTest,代码行数:12,代码来源:MvcForm.cs

示例7: GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound

        public void GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound()
        {
            // Arrange
            FormContext context = new FormContext();

            // Act
            FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName", true /* createIfNotFound */);

            // Assert
            Assert.NotNull(result);
            Assert.Equal("fieldName", result.FieldName);

            Assert.Single(context.FieldValidators);
            Assert.Equal(result, context.FieldValidators["fieldName"]);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:15,代码来源:FormContextTest.cs

示例8: Validate_AddsClientValidationMetadata

        public void Validate_AddsClientValidationMetadata()
        {
            var originalProviders = ModelValidatorProviders.Providers.ToArray();
            ModelValidatorProviders.Providers.Clear();

            try
            {
                // Arrange
                HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors());
                FormContext formContext = new FormContext()
                {
                    FormId = "form_id"
                };
                htmlHelper.ViewContext.ClientValidationEnabled = true;
                htmlHelper.ViewContext.FormContext = formContext;

                ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[]
                {
                    new ModelClientValidationRule() { ValidationType = "ValidationRule1" },
                    new ModelClientValidationRule() { ValidationType = "ValidationRule2" }
                };

                Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext);
                mockValidator.Setup(v => v.GetClientValidationRules())
                    .Returns(expectedValidationRules);
                Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>();
                mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>()))
                    .Returns(new[] { mockValidator.Object });
                ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object);

                // Act
                htmlHelper.Validate("baz");

                // Assert
                Assert.NotNull(formContext.GetValidationMetadataForField("baz"));
                Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray());
            }
            finally
            {
                ModelValidatorProviders.Providers.Clear();
                foreach (var provider in originalProviders)
                {
                    ModelValidatorProviders.Providers.Add(provider);
                }
            }
        }
开发者ID:AndreGleichner,项目名称:aspnetwebstack,代码行数:46,代码来源:ValidationExtensionsTest.cs

示例9: BeginFormSetsAndRestoresFormContext

        public void BeginFormSetsAndRestoresFormContext() {
            // Arrange
            Mock<HttpResponseBase> mockHttpResponse;
            HtmlHelper htmlHelper = GetFormHelper(@"<form action=""/some/path"" method=""post"">", @"</form>", out mockHttpResponse);

            FormContext originalContext = new FormContext();
            htmlHelper.ViewContext.FormContext = originalContext;

            // Act & assert - push
            MvcForm theForm = htmlHelper.BeginForm();
            Assert.IsNotNull(htmlHelper.ViewContext.FormContext);
            Assert.AreNotEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext should have been set to a new instance.");

            // Act & assert - pop
            theForm.Dispose();
            Assert.AreEqual(originalContext, htmlHelper.ViewContext.FormContext, "FormContext was not properly restored.");
        }
开发者ID:Marceli,项目名称:JQueryGridTest,代码行数:17,代码来源:FormExtensionsTest.cs

示例10: GetJsonValidationMetadata_ValidationSummary

        public void GetJsonValidationMetadata_ValidationSummary()
        {
            // Arrange
            FormContext context = new FormContext() { FormId = "theFormId", ValidationSummaryId = "validationSummary" };

            ModelClientValidationRule rule = new ModelClientValidationRule() { ValidationType = "ValidationType1", ErrorMessage = "Error Message" };
            rule.ValidationParameters["theParam"] = new { FirstName = "John", LastName = "Doe", Age = 32 };
            FieldValidationMetadata metadata = new FieldValidationMetadata() { FieldName = "theFieldName", ValidationMessageId = "theFieldName_ValidationMessage" };
            metadata.ValidationRules.Add(rule);
            context.FieldValidators["theFieldName"] = metadata;

            // Act
            string jsonMetadata = context.GetJsonValidationMetadata();

            // Assert
            string expected = @"{""Fields"":[{""FieldName"":""theFieldName"",""ReplaceValidationMessageContents"":false,""ValidationMessageId"":""theFieldName_ValidationMessage"",""ValidationRules"":[{""ErrorMessage"":""Error Message"",""ValidationParameters"":{""theParam"":{""FirstName"":""John"",""LastName"":""Doe"",""Age"":32}},""ValidationType"":""ValidationType1""}]}],""FormId"":""theFormId"",""ReplaceValidationSummary"":false,""ValidationSummaryId"":""validationSummary""}";
            Assert.Equal(expected, jsonMetadata);
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:18,代码来源:FormContextTest.cs

示例11: OutputClientValidation

        private static void OutputClientValidation(HttpResponseBase response, FormContext formContext) {
            if (!formContext.ClientValidationEnabled) {
                return; // do nothing
            }

            // output a call that resembles:
            // _clientValidationFunction(validationObject, userContext);

            string validationJson = formContext.GetJsonValidationMetadata();

            string userContextJson = (formContext.ClientValidationState != null)
                ? new JavaScriptSerializer().Serialize(formContext.ClientValidationState)
                : "null";

            string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);
            string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines,
                formContext.ClientValidationFunction, validationJson, userContextJson);

            response.Write(formatted);
        }
开发者ID:Marceli,项目名称:JQueryGridTest,代码行数:20,代码来源:MvcForm.cs

示例12: GetUnobtrusiveValidationAttributeUsesViewDataForModelMetadataLookup

        public void GetUnobtrusiveValidationAttributeUsesViewDataForModelMetadataLookup()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock<ViewContext>();
            var viewData = new ViewDataDictionary<MyModel>();
            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            viewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
            var viewDataContainer = MvcHelper.GetViewDataContainer(viewData);
            var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer);

            // Act
            IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("MyProperty");

            // Assert
            Assert.Equal(2, result.Count);
            Assert.Equal("true", result["data-val"]);
            Assert.Equal("My required message", result["data-val-required"]);
        }
开发者ID:tlycken,项目名称:aspnetwebstack,代码行数:21,代码来源:HtmlHelperTest.cs

示例13: GetUnobtrusiveValidationAttributesUsesShortNameForModelMetadataLookup

        public void GetUnobtrusiveValidationAttributesUsesShortNameForModelMetadataLookup()
        {
            // Arrange
            string passedName = null;
            var formContext = new FormContext();
            var viewContext = new Mock<ViewContext>();
            var viewData = new ViewDataDictionary();
            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            viewData.TemplateInfo.HtmlFieldPrefix = "Prefix";
            var viewDataContainer = MvcHelper.GetViewDataContainer(viewData);
            var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer);
            htmlHelper.ClientValidationRuleFactory = (name, _) =>
            {
                passedName = name;
                return Enumerable.Empty<ModelClientValidationRule>();
            };

            // Act
            htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.Equal("foobar", passedName);
        }
开发者ID:tlycken,项目名称:aspnetwebstack,代码行数:25,代码来源:HtmlHelperTest.cs

示例14: GetUnobtrusiveValidationAttributesWithTwoClientRules

        public void GetUnobtrusiveValidationAttributesWithTwoClientRules()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock<ViewContext>();
            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer);
            htmlHelper.ClientValidationRuleFactory = delegate
            {
                ModelClientValidationRule rule1 = new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "error" };
                rule1.ValidationParameters["foo"] = "bar";
                rule1.ValidationParameters["baz"] = "biff";
                ModelClientValidationRule rule2 = new ModelClientValidationRule { ValidationType = "othertype", ErrorMessage = "othererror" };
                rule2.ValidationParameters["true3"] = "false4";
                return new[] { rule1, rule2 };
            };

            // Act
            IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.Equal("error", result["data-val-type"]);
            Assert.Equal("bar", result["data-val-type-foo"]);
            Assert.Equal("biff", result["data-val-type-baz"]);
            Assert.Equal("othererror", result["data-val-othertype"]);
            Assert.Equal("false4", result["data-val-othertype-true3"]);
        }
开发者ID:tlycken,项目名称:aspnetwebstack,代码行数:30,代码来源:HtmlHelperTest.cs

示例15: GetUnobtrusiveValidationAttributesMessageIsNotHtmlEncoded

        public void GetUnobtrusiveValidationAttributesMessageIsNotHtmlEncoded()
        {
            // Arrange
            var formContext = new FormContext();
            var viewContext = new Mock<ViewContext>();
            viewContext.SetupGet(vc => vc.FormContext).Returns(formContext);
            viewContext.SetupGet(vc => vc.ClientValidationEnabled).Returns(true);
            viewContext.SetupGet(vc => vc.UnobtrusiveJavaScriptEnabled).Returns(true);
            var viewDataContainer = MvcHelper.GetViewDataContainer(new ViewDataDictionary());
            var htmlHelper = new HtmlHelper(viewContext.Object, viewDataContainer);
            htmlHelper.ClientValidationRuleFactory = delegate { return new[] { new ModelClientValidationRule { ValidationType = "type", ErrorMessage = "<script>alert('xss')</script>" } }; };

            // Act
            IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");

            // Assert
            Assert.Equal("<script>alert('xss')</script>", result["data-val-type"]);
        }
开发者ID:tlycken,项目名称:aspnetwebstack,代码行数:18,代码来源:HtmlHelperTest.cs


注:本文中的FormContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。