Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Custom Class for custom attributes
- public class MyHtmlHelper<TModel>
- {
- private readonly HtmlHelper<TModel> htmlHelper;
- internal MyHtmlHelper(HtmlHelper<TModel> htmlHelper)
- {
- this.htmlHelper = htmlHelper;
- }
- //Here the routeValues parameter of Begin Form is passed directly to the method as null
- public MvcForm MyBeginForm()
- {
- var myAttributes = new Dictionary<string, object>(){
- {"test", "value"},
- {"test2", "value2"},
- };
- return htmlHelper.BeginForm("Index", "Home", null, FormMethod.Post, myAttributes);
- }
- //Here I have passed the null value through the parameter
- public MvcForm MyBeginForm(object routeValues)
- {
- var myAttributes = new Dictionary<string, object>(){
- {"test", "value"},
- {"test2", "value2"},
- };
- return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
- }
- }
- //This class is used for static call in html
- public static class MyHtmlHelperkEx
- {
- public static MyHtmlHelper<TModel> MyHtmlHelper<TModel>(this HtmlHelper<TModel> htmlHelper)
- {
- return new MyHtmlHelper<TModel>(htmlHelper);
- }
- }
- <h1>Without Parameter</h1>
- @using (Html.MyHtmlHelper().MyBeginForm()) { }
- <h1>With parmeter</h1>
- @using (Html.MyHtmlHelper().MyBeginForm(null)) { }
- <h1>Without Parameter</h1>
- <form action="/" method="post" test="value" test2="value2">
- System.Web.Mvc.Html.MvcForm
- </form>
- <h1>With parmeter</h1>
- <form comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" count="2" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" action="/" method="post"></form>
- return htmlHelper.BeginForm("Index", "Home", (object)null, FormMethod.Post, myAttributes);
- public MvcForm MyBeginForm(object routeValues)
- {
- var myAttributes = new{
- test = "value",
- test2 = "value2",
- };
- return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
- }
- public MvcForm MyBeginForm(RouteValueDictionary routeValues)
- {
- var myAttributes = new Dictionary<string, object>(){
- {"test", "value"},
- {"test2", "value2"},
- };
- return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
- }
- <form action="/" method="post" test="value" test2="value2"></form>
- public MvcForm MyBeginForm()
- {
- return MyBeginForm(null);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement