1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Web ;
5+ using System . Web . Mvc ;
6+ using System . Web . Routing ;
7+
8+ namespace BookCollection . Helpers
9+ {
10+ //http://stackoverflow.com/questions/15952240/how-do-you-override-html-actionlink
11+
12+ public class CustomHtmlHelper < T > : HtmlHelper < T >
13+ {
14+ public CustomHtmlHelper ( ViewContext viewContext , IViewDataContainer viewDataContainer ) :
15+ base ( viewContext , viewDataContainer )
16+ {
17+ }
18+
19+ //Instance methods will always be called instead of extension methods when both exist with the same signature...
20+
21+ public MvcHtmlString ActionLink ( string linkText , string actionName )
22+ {
23+ return ActionLink ( linkText , actionName , null , new RouteValueDictionary ( ) , new RouteValueDictionary ( ) ) ;
24+ }
25+
26+ public MvcHtmlString ActionLink ( string linkText , string actionName , object routeValues )
27+ {
28+ return ActionLink ( linkText , actionName , null , new RouteValueDictionary ( routeValues ) , new RouteValueDictionary ( ) ) ;
29+ }
30+
31+ public MvcHtmlString ActionLink ( string linkText , string actionName , string controllerName )
32+ {
33+ return ActionLink ( linkText , actionName , controllerName , new RouteValueDictionary ( ) , new RouteValueDictionary ( ) ) ;
34+ }
35+
36+ public MvcHtmlString ActionLink ( string linkText , string actionName , RouteValueDictionary routeValues )
37+ {
38+ return ActionLink ( linkText , actionName , null , routeValues , new RouteValueDictionary ( ) ) ;
39+ }
40+
41+ public MvcHtmlString ActionLink ( string linkText , string actionName , object routeValues , object htmlAttributes )
42+ {
43+ return ActionLink ( linkText , actionName , null , new RouteValueDictionary ( routeValues ) , AnonymousObjectToHtmlAttributes ( htmlAttributes ) ) ;
44+ }
45+
46+ public MvcHtmlString ActionLink ( string linkText , string actionName , RouteValueDictionary routeValues , IDictionary < string , object > htmlAttributes )
47+ {
48+ return ActionLink ( linkText , actionName , null , routeValues , htmlAttributes ) ;
49+ }
50+
51+ public MvcHtmlString ActionLink ( string linkText , string actionName , string controllerName , object routeValues , object htmlAttributes )
52+ {
53+ return ActionLink ( linkText , actionName , controllerName , new RouteValueDictionary ( routeValues ) , AnonymousObjectToHtmlAttributes ( htmlAttributes ) ) ;
54+ }
55+
56+ public MvcHtmlString ActionLink ( string linkText , string actionName , string controllerName , RouteValueDictionary routeValues , IDictionary < string , object > htmlAttributes )
57+ {
58+ string sortIconHtml = "" ;
59+ routeValues = SetSortParam ( routeValues , out sortIconHtml ) ;
60+ string link = GenerateLink ( ViewContext . RequestContext , RouteCollection , linkText , null , actionName , controllerName , routeValues , htmlAttributes ) ;
61+
62+ return MvcHtmlString . Create ( sortIconHtml + link ) ;
63+ }
64+
65+ public MvcHtmlString ActionLink ( string linkText , string actionName , string controllerName , string protocol , string hostName , string fragment , object routeValues , object htmlAttributes )
66+ {
67+ return ActionLink ( linkText , actionName , controllerName , protocol , hostName , fragment , new RouteValueDictionary ( routeValues ) , AnonymousObjectToHtmlAttributes ( htmlAttributes ) ) ;
68+ }
69+
70+ public MvcHtmlString ActionLink ( string linkText , string actionName , string controllerName , string protocol , string hostName , string fragment , RouteValueDictionary routeValues , IDictionary < string , object > htmlAttributes )
71+ {
72+ string sortIconHtml = "" ;
73+ routeValues = SetSortParam ( routeValues , out sortIconHtml ) ;
74+ string link = GenerateLink ( ViewContext . RequestContext , RouteCollection , linkText , null , actionName , controllerName , protocol , hostName , fragment , routeValues , htmlAttributes ) ;
75+
76+ return MvcHtmlString . Create ( sortIconHtml + link ) ;
77+ }
78+
79+ private const string spanTemplate = "<span class=\" glyphicon glyphicon-{0}\" aria-hidden=\" true\" ></span>" ;
80+ public RouteValueDictionary SetSortParam ( RouteValueDictionary rvd , out string sortPrefix )
81+ {
82+ sortPrefix = "" ;
83+ // Is sortable link?
84+ if ( rvd != null && rvd . Keys . Contains ( "sortKey" ) && ! string . IsNullOrWhiteSpace ( rvd [ "sortKey" ] as string ) )
85+ {
86+ // prevent double keys
87+ if ( rvd . ContainsKey ( "sortOrder" ) )
88+ {
89+ rvd . Remove ( "sortOrder" ) ;
90+ }
91+
92+ string sortKey = rvd [ "sortKey" ] as string ;
93+ // Has user specified a sort?
94+ if ( rvd . Keys . Contains ( "currentSort" ) && ! string . IsNullOrWhiteSpace ( rvd [ "currentSort" ] as string ) )
95+ {
96+ string [ ] currentSort = ( rvd [ "currentSort" ] as string ) . Split ( new [ ] { '_' } ) ;
97+ if ( currentSort . Length == 2 )
98+ {
99+ // Is specified current sort matching the sort key for this link?
100+ if ( sortKey . Equals ( currentSort [ 0 ] , StringComparison . InvariantCultureIgnoreCase ) )
101+ {
102+
103+ if ( currentSort [ 1 ] . Equals ( "desc" , StringComparison . InvariantCultureIgnoreCase ) )
104+ {
105+ // set new sort direction and display icon ascending
106+ sortPrefix = string . Format ( spanTemplate , "sort-by-attributes-alt" ) ;
107+ rvd . Add ( "sortOrder" , sortKey + "_asc" ) ;
108+ }
109+ else
110+ {
111+ // set new sort direction and display icon descending
112+ sortPrefix = string . Format ( spanTemplate , "sort-by-attributes" ) ;
113+ rvd . Add ( "sortOrder" , sortKey + "_desc" ) ;
114+ }
115+ }
116+ else
117+ {
118+ rvd . Add ( "sortOrder" , sortKey + "_asc" ) ;
119+ }
120+ }
121+ }
122+ else
123+ {
124+ // No used sort, set default first sort for this sort key
125+ rvd . Add ( "sortOrder" , sortKey + "_asc" ) ;
126+ }
127+ }
128+ return rvd ;
129+ }
130+ }
131+ }
0 commit comments