本文整理匯總了C#中System.HtmlHelper.GetUnobtrusiveValidationAttributes方法的典型用法代碼示例。如果您正苦於以下問題:C# HtmlHelper.GetUnobtrusiveValidationAttributes方法的具體用法?C# HtmlHelper.GetUnobtrusiveValidationAttributes怎麽用?C# HtmlHelper.GetUnobtrusiveValidationAttributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.HtmlHelper
的用法示例。
在下文中一共展示了HtmlHelper.GetUnobtrusiveValidationAttributes方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetUnobtrusiveValidationAttributesMarksRenderedFieldsWithFullName
public void GetUnobtrusiveValidationAttributesMarksRenderedFieldsWithFullName() {
// Arrange
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);
// Act
htmlHelper.GetUnobtrusiveValidationAttributes("foobar");
// Assert
Assert.IsFalse(formContext.RenderedField("foobar"));
Assert.IsTrue(formContext.RenderedField("Prefix.foobar"));
}
示例2: AssertBadClientValidationRule
// Helpers
private static void AssertBadClientValidationRule(HtmlHelper htmlHelper, string expectedMessage, params ModelClientValidationRule[] rules) {
htmlHelper.ClientValidationRuleFactory = delegate { return rules; };
ExceptionHelper.ExpectInvalidOperationException(
() => htmlHelper.GetUnobtrusiveValidationAttributes(Guid.NewGuid().ToString()),
expectedMessage
);
}
示例3: 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.AreEqual("foobar", passedName);
}
示例4: 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.AreEqual(2, result.Count);
Assert.AreEqual("true", result["data-val"]);
Assert.AreEqual("My required message", result["data-val-required"]);
}
示例5: 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.AreEqual("error", result["data-val-type"]);
Assert.AreEqual("bar", result["data-val-type-foo"]);
Assert.AreEqual("biff", result["data-val-type-baz"]);
Assert.AreEqual("othererror", result["data-val-othertype"]);
Assert.AreEqual("false4", result["data-val-othertype-true3"]);
}
示例6: GetUnobtrusiveValidationAttributesMessageIsHtmlEncoded
public void GetUnobtrusiveValidationAttributesMessageIsHtmlEncoded() {
// 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.AreEqual("<script>alert('xss')</script>", result["data-val-type"]);
}
示例7: GetUnobtrusiveValidationAttributesIncludesDataValTrueWithNonEmptyClientRuleList
public void GetUnobtrusiveValidationAttributesIncludesDataValTrueWithNonEmptyClientRuleList() {
// 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" } };
};
// Act
IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");
// Assert
Assert.AreEqual("true", result["data-val"]);
}
示例8: GetUnobtrusiveValidationAttributesReturnsEmptySetAndSetsFieldAsRenderedForFieldWithNoClientRules
public void GetUnobtrusiveValidationAttributesReturnsEmptySetAndSetsFieldAsRenderedForFieldWithNoClientRules() {
// 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 Enumerable.Empty<ModelClientValidationRule>(); };
// Act
IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");
// Assert
Assert.AreEqual(0, result.Count);
Assert.IsTrue(formContext.RenderedField("foobar"));
}
示例9: GetUnobtrusiveValidationAttributesReturnsEmptySetWhenFieldHasAlreadyBeenRendered
public void GetUnobtrusiveValidationAttributesReturnsEmptySetWhenFieldHasAlreadyBeenRendered() {
// Arrange
var formContext = new FormContext();
formContext.RenderedField("foobar", true);
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);
// Act
IDictionary<string, object> result = htmlHelper.GetUnobtrusiveValidationAttributes("foobar");
// Assert
Assert.AreEqual(0, result.Count);
}