using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; namespace Zm.Common.Extension { public static class FormExtensions { private const string RETURN_URL_PARA = "ReturnUrl"; public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper) { return htmlHelper.BeginReturnUrlForm(new RouteValueDictionary()); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, object routeValues) { return htmlHelper.BeginReturnUrlForm(new RouteValueDictionary(routeValues)); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, RouteValueDictionary routeValues) { routeValues.AddReturnUrl(htmlHelper); return htmlHelper.BeginForm(routeValues); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, new RouteValueDictionary()); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, method, new RouteValueDictionary()); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, routeValues, FormMethod.Post); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, routeValues, FormMethod.Post); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, IDictionary htmlAttributes) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, new RouteValueDictionary(), method, htmlAttributes); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, FormMethod method, object htmlAttributes) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, new RouteValueDictionary(), method, CreateHtmlAttributeDictionary(htmlAttributes)); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, new RouteValueDictionary(routeValues), method); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, routeValues, method, null); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes) { return htmlHelper.BeginReturnUrlForm(actionName, controllerName, new RouteValueDictionary(routeValues), method, CreateHtmlAttributeDictionary(htmlAttributes)); } public static MvcForm BeginReturnUrlForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary htmlAttributes) { routeValues.AddReturnUrl(htmlHelper); return htmlHelper.BeginReturnUrlForm(actionName, controllerName, routeValues, method, htmlAttributes); } private static string ReturnUrl(this HtmlHelper htmlHelper) { return htmlHelper.ViewBag.ReturnUrl; } private static void AddReturnUrl(this RouteValueDictionary dic, HtmlHelper htmlHelper) { if (!dic.ContainsKey(RETURN_URL_PARA)) { var returnUrl = htmlHelper.ReturnUrl(); if (!string.IsNullOrWhiteSpace(returnUrl)) { dic.Add(RETURN_URL_PARA, returnUrl); } } } private static RouteValueDictionary CreateRouteValueDictionayWithReturnUrl(HtmlHelper htmlHelper, object routeValues = null) { var routeValuesDic = new RouteValueDictionary(routeValues); routeValuesDic.AddReturnUrl(htmlHelper); return routeValuesDic; } private static RouteValueDictionary CreateHtmlAttributeDictionary(object htmlAttributes) { return HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); } } }