C# - BeginForm Method for HTML Attributes only

By , 4/5/2015
(0 ratings)
This extension method works like the other BeginForm methods from MVC web projects, but takes only a dictionary with HTML attributes.
The methods are placed in the same namespace as the other BeginForm methods.
namespace System.Web.Mvc.Html
{
    using System.Collections.Generic;

    public static class MyFormExtensions
    {
        /// <summary>
        /// Writes an opening &lt;form> tag to the response. The form uses the specified HTML attributes from the dictionary.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
        /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
        /// <returns>An opening &lt;form> tag.</returns>
        /// <seealso cref="https://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform.aspx"/>
        public static MvcForm BeginForm2(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
        {
            return htmlHelper.BeginForm(null, null, FormMethod.Post, htmlAttributes);
        }
        /// <summary>
        /// Writes an opening &lt;form> tag to the response. The form uses the specified HTML attributes from the dictionary.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper instance that this method extends.</param>
        /// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
        /// <returns>An opening &lt;form> tag.</returns>
        /// <seealso cref="https://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform.aspx"/>
        public static MvcForm BeginForm2(this HtmlHelper htmlHelper, object htmlAttributes)
        {
            return htmlHelper.BeginForm(null, null, FormMethod.Post, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    }
}

Tagged with MVC, BeginForm.

Comments

 

Log in, to comment!

Related Snippets