PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MvcFutures/Mvc/FormExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 41 lines | 36 code | 5 blank | 0 comment | 0 complexity | 9537269599d78c008ee263e4c3476505 MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq.Expressions;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Mvc.Html;
  9. using System.Web.Routing;
  10. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  11. public static class FormExtensions {
  12. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
  13. public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController : Controller {
  14. return BeginForm<TController>(helper, action, FormMethod.Post, null);
  15. }
  16. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
  17. public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method) where TController : Controller {
  18. return BeginForm<TController>(helper, action, method, null);
  19. }
  20. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
  21. public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, object htmlAttributes) where TController : Controller {
  22. return BeginForm<TController>(helper, action, method, new RouteValueDictionary(htmlAttributes));
  23. }
  24. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
  25. public static MvcForm BeginForm<TController>(this HtmlHelper helper, Expression<Action<TController>> action, FormMethod method, IDictionary<string, object> htmlAttributes) where TController : Controller {
  26. TagBuilder tagBuilder = new TagBuilder("form");
  27. tagBuilder.MergeAttributes(htmlAttributes);
  28. string formAction = LinkExtensions.BuildUrlFromExpression(helper, action);
  29. tagBuilder.MergeAttribute("action", formAction);
  30. tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method));
  31. HttpResponseBase httpResponse = helper.ViewContext.HttpContext.Response;
  32. httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));
  33. return new MvcForm(helper.ViewContext.HttpContext.Response);
  34. }
  35. }
  36. }