// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; namespace Microsoft.Web.Mvc { public static class FormExtensions { [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcForm BeginForm(this HtmlHelper helper, Expression> action) where TController : Controller { return BeginForm(helper, action, FormMethod.Post, null); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcForm BeginForm(this HtmlHelper helper, Expression> action, FormMethod method) where TController : Controller { return BeginForm(helper, action, method, null); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcForm BeginForm(this HtmlHelper helper, Expression> action, FormMethod method, object htmlAttributes) where TController : Controller { return BeginForm(helper, action, method, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); } [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")] public static MvcForm BeginForm(this HtmlHelper helper, Expression> action, FormMethod method, IDictionary htmlAttributes) where TController : Controller { TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes(htmlAttributes); string formAction = helper.BuildUrlFromExpression(action); tagBuilder.MergeAttribute("action", formAction); tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method)); helper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); return new MvcForm(helper.ViewContext); } } }