wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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<string, object> 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<string, object> 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);
        }
    }
}