Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. //Custom Class for custom attributes
  2. public class MyHtmlHelper<TModel>
  3. {
  4. private readonly HtmlHelper<TModel> htmlHelper;
  5.  
  6. internal MyHtmlHelper(HtmlHelper<TModel> htmlHelper)
  7. {
  8. this.htmlHelper = htmlHelper;
  9. }
  10.  
  11. //Here the routeValues parameter of Begin Form is passed directly to the method as null
  12. public MvcForm MyBeginForm()
  13. {
  14. var myAttributes = new Dictionary<string, object>(){
  15. {"test", "value"},
  16. {"test2", "value2"},
  17. };
  18.  
  19. return htmlHelper.BeginForm("Index", "Home", null, FormMethod.Post, myAttributes);
  20. }
  21.  
  22. //Here I have passed the null value through the parameter
  23. public MvcForm MyBeginForm(object routeValues)
  24. {
  25. var myAttributes = new Dictionary<string, object>(){
  26. {"test", "value"},
  27. {"test2", "value2"},
  28. };
  29.  
  30. return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
  31. }
  32. }
  33.  
  34. //This class is used for static call in html
  35. public static class MyHtmlHelperkEx
  36. {
  37. public static MyHtmlHelper<TModel> MyHtmlHelper<TModel>(this HtmlHelper<TModel> htmlHelper)
  38. {
  39. return new MyHtmlHelper<TModel>(htmlHelper);
  40. }
  41. }
  42.  
  43. <h1>Without Parameter</h1>
  44. @using (Html.MyHtmlHelper().MyBeginForm()) { }
  45.  
  46. <h1>With parmeter</h1>
  47. @using (Html.MyHtmlHelper().MyBeginForm(null)) { }
  48.  
  49. <h1>Without Parameter</h1>
  50. <form action="/" method="post" test="value" test2="value2">
  51. System.Web.Mvc.Html.MvcForm
  52. </form>
  53.  
  54. <h1>With parmeter</h1>
  55. <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>
  56.  
  57. return htmlHelper.BeginForm("Index", "Home", (object)null, FormMethod.Post, myAttributes);
  58.  
  59. public MvcForm MyBeginForm(object routeValues)
  60. {
  61. var myAttributes = new{
  62. test = "value",
  63. test2 = "value2",
  64. };
  65. return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
  66. }
  67.  
  68. public MvcForm MyBeginForm(RouteValueDictionary routeValues)
  69. {
  70. var myAttributes = new Dictionary<string, object>(){
  71. {"test", "value"},
  72. {"test2", "value2"},
  73. };
  74. return htmlHelper.BeginForm("Index", "Home", routeValues, FormMethod.Post, myAttributes);
  75. }
  76.  
  77. <form action="/" method="post" test="value" test2="value2"></form>
  78.  
  79. public MvcForm MyBeginForm()
  80. {
  81. return MyBeginForm(null);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement