本文整理汇总了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());
}
示例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();
}
示例3: ReplaceValidationSummaryProperty
public void ReplaceValidationSummaryProperty()
{
// Arrange
FormContext context = new FormContext();
// Act & Assert
MemberHelper.TestBooleanProperty(context, "ReplaceValidationSummary", false, false);
}
示例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();
}
示例5: FieldValidatorsProperty
public void FieldValidatorsProperty()
{
// Arrange
FormContext context = new FormContext();
// Act
IDictionary<String, FieldValidationMetadata> fieldValidators = context.FieldValidators;
// Assert
Assert.NotNull(fieldValidators);
Assert.Empty(fieldValidators);
}
示例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();
}
示例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"]);
}
示例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);
}
}
}
示例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.");
}
示例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);
}
示例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);
}
示例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"]);
}
示例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);
}
示例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"]);
}
示例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"]);
}