本文整理汇总了C#中FormMethod.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# FormMethod.ToString方法的具体用法?C# FormMethod.ToString怎么用?C# FormMethod.ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FormMethod
的用法示例。
在下文中一共展示了FormMethod.ToString方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormContainer
public FormContainer(HtmlTextWriter htmlTextWriter,
string url ,
BootstrapFormType formType = BootstrapFormType.Horizontal,
FormMethod method = FormMethod.Post,
object htmlAttributes = null)
{
this.textWriter = htmlTextWriter;
this.formType = formType;
textWriter.AddAttribute("method", method.ToString());
textWriter.AddAttribute("action", url ) ;
var htmlAttributesCollection = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
switch (this.formType)
{
case BootstrapFormType.Horizontal: htmlAttributesCollection.Merge("class", "form-horizontal"); break;
case BootstrapFormType.Vertical: htmlAttributesCollection.Merge("class", "form-vertical"); break;
case BootstrapFormType.Inline: htmlAttributesCollection.Merge("class", "form-inline"); break;
}
foreach (var htmlAttribute in htmlAttributesCollection)
{
textWriter.AddAttribute(htmlAttribute.Key, htmlAttribute.Value.ToString());
}
textWriter.RenderBeginTag(HtmlTextWriterTag.Form);
}
示例2: Button
public static MvcHtmlString Button(this HtmlHelper helper,
string text, string urlAction, FormMethod method, object htmlAttributes)
{
var tagBuilder = new TagBuilder("input");
FillWithAttributes(tagBuilder, new { type = "submit", formaction = urlAction, formmethod = method.ToString(), value = text });
FillWithAttributes(tagBuilder, htmlAttributes);
return new MvcHtmlString(tagBuilder.ToString());
}
示例3: BeginRouteFormWithRouteNameMethodAndHtmlAttributesParameters_CallsHtmlGeneratorWithExpectedValues
public void BeginRouteFormWithRouteNameMethodAndHtmlAttributesParameters_CallsHtmlGeneratorWithExpectedValues(
string routeName,
FormMethod method,
object htmlAttributes)
{
// Arrange
var tagBuilder = new TagBuilder(tagName: "form");
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(htmlGenerator.Object);
htmlGenerator
.Setup(g => g.GenerateRouteForm(
htmlHelper.ViewContext,
routeName,
null, // routeValues
method.ToString().ToLowerInvariant(),
htmlAttributes))
.Returns(tagBuilder)
.Verifiable();
if (method != FormMethod.Get)
{
htmlGenerator
.Setup(g => g.GenerateAntiforgery(htmlHelper.ViewContext))
.Returns(HtmlString.Empty)
.Verifiable();
}
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
// Act
var mvcForm = htmlHelper.BeginRouteForm(routeName, method, htmlAttributes);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal("<form>", builder.ToString());
htmlGenerator.Verify();
}
示例4: TagBuilder
public void BeginFormWithMethodAndHtmlAttributesParameters_SuppressAntiforgery_CallsHtmlGeneratorWithExpectedValues(
FormMethod method,
object htmlAttributes)
{
// Arrange
var tagBuilder = new TagBuilder(tagName: "form");
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(htmlGenerator.Object);
htmlGenerator
.Setup(g => g.GenerateForm(
htmlHelper.ViewContext,
null, // actionName
null, // controllerName
null, // routeValues
method.ToString().ToLowerInvariant(),
htmlAttributes))
.Returns(tagBuilder)
.Verifiable();
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
// Act
var mvcForm = htmlHelper.BeginForm(method, antiforgery: false, htmlAttributes: htmlAttributes);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal("<form>", builder.ToString());
htmlGenerator.Verify();
}
示例5: ProcessAsync_GeneratesAntiforgeryCorrectly
public async Task ProcessAsync_GeneratesAntiforgeryCorrectly(
bool? antiforgery,
FormMethod method,
string expectedPostContent)
{
// Arrange
var viewContext = CreateViewContext();
var expectedAttribute = new TagHelperAttribute("method", method.ToString().ToLowerInvariant());
var context = new TagHelperContext(
allAttributes: new TagHelperAttributeList(new[] { expectedAttribute }),
items: new Dictionary<object, object>(),
uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult, encoder) =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator
.Setup(mock => mock.GenerateForm(
It.IsAny<ViewContext>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<object>(),
It.IsAny<string>(),
It.IsAny<object>()))
.Returns(new TagBuilder("form"));
generator.Setup(mock => mock.GenerateAntiforgery(viewContext))
.Returns(new HtmlString("<input />"));
var formTagHelper = new FormTagHelper(generator.Object)
{
Action = "Index",
Antiforgery = antiforgery,
ViewContext = viewContext,
Method = method.ToString().ToLowerInvariant()
};
// Act
await formTagHelper.ProcessAsync(context, output);
// Assert
Assert.Equal("form", output.TagName);
Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
var attribute = Assert.Single(output.Attributes);
Assert.Equal(expectedAttribute, attribute);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.GetContent().Length == 0);
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
}
示例6: BeginRouteForm_RendersExpectedValues
public void BeginRouteForm_RendersExpectedValues(
string routeName,
object routeValues,
FormMethod method,
object htmlAttributes)
{
// Arrange
var expectedAction = "http://localhost/Hello/World";
var expectedStartTag = string.Format(
"<form action=\"HtmlEncode[[{0}]]\" method=\"HtmlEncode[[{1}]]\"{2}>",
expectedAction,
method.ToString().ToLowerInvariant(),
GetHtmlAttributesAsString(htmlAttributes));
var urlHelper = new Mock<IUrlHelper>(MockBehavior.Strict);
urlHelper
.Setup(realHelper => realHelper.RouteUrl(It.Is<UrlRouteContext>(context =>
string.Equals(context.RouteName, routeName) &&
context.Values == routeValues &&
context.Protocol == null &&
context.Host == null &&
context.Fragment == null)))
.Returns(expectedAction)
.Verifiable();
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(urlHelper.Object);
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
// Act
var mvcForm = htmlHelper.BeginRouteForm(routeName, routeValues, method, htmlAttributes);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal(expectedStartTag, builder.ToString());
urlHelper.Verify();
}
示例7: BeginRouteFormWithRouteNameRouteValuesAndMethodParameters_CallsHtmlGeneratorWithExpectedValues
public void BeginRouteFormWithRouteNameRouteValuesAndMethodParameters_CallsHtmlGeneratorWithExpectedValues(
string routeName,
object routeValues,
FormMethod method)
{
// Arrange
var tagBuilder = new TagBuilder(tagName: "form", htmlEncoder: new NullTestEncoder());
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(htmlGenerator.Object);
htmlGenerator
.Setup(realHelper => realHelper.GenerateRouteForm(
htmlHelper.ViewContext,
routeName,
routeValues,
method.ToString().ToLowerInvariant(),
null)) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
// Act
var mvcForm = htmlHelper.BeginRouteForm(routeName, routeValues, method);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal("<form>", builder.ToString());
htmlGenerator.Verify();
}
示例8: TagBuilder
public void BeginFormWithActionNameContollerNameRouteValuesAndMethodParameters_CallsHtmlGeneratorWithExpectedValues(
string actionName,
string controllerName,
object routeValues,
FormMethod method)
{
// Arrange
var tagBuilder = new TagBuilder(tagName: "form");
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var htmlHelper = DefaultTemplatesUtilities.GetHtmlHelper(htmlGenerator.Object);
htmlGenerator
.Setup(g => g.GenerateForm(
htmlHelper.ViewContext,
actionName,
controllerName,
routeValues,
method.ToString().ToLowerInvariant(),
null)) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
htmlGenerator
.Setup(g => g.GenerateAntiforgery(htmlHelper.ViewContext))
.Returns(HtmlString.Empty)
.Verifiable();
// Guards
Assert.NotNull(htmlHelper.ViewContext);
var writer = Assert.IsAssignableFrom<StringWriter>(htmlHelper.ViewContext.Writer);
var builder = writer.GetStringBuilder();
Assert.NotNull(builder);
// Act
var mvcForm = htmlHelper.BeginForm(actionName, controllerName, routeValues, method);
// Assert
Assert.NotNull(mvcForm);
Assert.Equal("<form>", builder.ToString());
htmlGenerator.Verify();
}
示例9: PackFormMethod
public static String PackFormMethod(FormMethod formMethod)
{
return "method=\"" + formMethod.ToString() + "\"";
}