.net mvc HtmlHelper扩展使用
如果是你是从webform开始接触.net,你应该记得webform开发中,存在自定义控件这东西,它使得我们开发起来十分方便,如今mvc大势所趋,其实在mvc开发时,也存在自定义控件这么个东西,那就是HtmlHelper扩展,如果你们已经使用过Razor,那你们基本都用过:@Html.BeginForm(),@Html.DropDownList()等基本的控件,但是这些控件肯定无法满足我们的需求,所以我们需要自定义一些扩展,我这写了一些扩展:
1 public static class HtmlHelperExtensions 2 { 3 public static MvcHtmlString ConvertToEnumDesc(this HtmlHelper helper, object enumValue) 4 { 5 return new MvcHtmlString(EnumDescription.GetFieldText(enumValue)); 6 } 7 8 public static MvcHtmlString ConvertTimestampToDateString(this HtmlHelper helper, long timestamp, string DateFormat = "yyyy-MM-dd HH:mm:ss") 9 { 10 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 11 long lTime = timestamp * 10000000; 12 TimeSpan toNow = new TimeSpan(lTime); 13 14 return new MvcHtmlString(dtStart.Add(toNow).ToString(DateFormat)); 15 } 16 17 public static MvcHtmlString NewMenuLink(this HtmlHelper helper, string linkText, string actionName, 18 string controllerName, object routeValues = null) 19 { 20 string currentAction = helper.ViewContext.RouteData.GetRequiredString("action"); 21 string currentController = helper.ViewContext.RouteData.GetRequiredString("controller"); 22 23 //modified this to work whenever a view of the controller is selected 24 //if (actionName == currentAction && controllerName == currentController) 25 26 //var strLink = helper.ActionLink(linkText, actionName, controllerName, routeValues, null); 27 28 var result = new TagBuilder("a"); 29 var url = UrlHelper.GenerateUrl(null, actionName, controllerName, new RouteValueDictionary(routeValues), helper.RouteCollection, 30 helper.ViewContext.RequestContext, true); 31 result.Attributes.Add("href", url); 32 result.InnerHtml = "<i class=\"fa fa-file-o\"></i>" + linkText; 33 34 35 var strResult = ""; 36 if (controllerName.ToLower() == currentController.ToLower() && actionName.ToLower() == currentAction.ToLower()) 37 { 38 strResult = "<li class=\"active\">" + result.ToString() + "</li>"; 39 } 40 else 41 { 42 strResult = "<li>" + result.ToString() + "</li>"; 43 } 44 return new MvcHtmlString(strResult); 45 } 46 47 public static MvcHtmlString MenuLink(this HtmlHelper helper, string linkText, string actionName, 48 string controllerName, object routeValues = null) 49 { 50 string currentAction = helper.ViewContext.RouteData.GetRequiredString("action"); 51 string currentController = helper.ViewContext.RouteData.GetRequiredString("controller"); 52 53 //modified this to work whenever a view of the controller is selected 54 //if (actionName == currentAction && controllerName == currentController) 55 56 var strLink = helper.ActionLink(linkText, actionName, controllerName, routeValues, null); 57 58 59 var strResult = ""; 60 if (controllerName.ToLower() == currentController.ToLower() && actionName.ToLower() == currentAction.ToLower()) 61 { 62 strResult = "<li class=\"active\">" + strLink.ToString() + "</li>"; 63 } 64 else 65 { 66 strResult = "<li>" + strLink.ToString() + "</li>"; 67 } 68 return new MvcHtmlString(strResult); 69 } 70 }
那么问题就来了,这些扩展如何来使用?
1 <div class="container-fluid"> 2 <div class="row-fluid"> 3 <div class="span2"> 4 <div class="well sidebar-nav"> 5 <ul class="nav nav-list"> 6 @Html.MenuLink("站点配置", "Index", "Sites") 7 @Html.MenuLink("添加站点", "Add", "Sites") 8 @Html.MenuLink("用户属性配置", "ServiceAttributeList", "Sites") 9 @Html.MenuLink("通行证配置", "PassportServerConfig", "Sites") 10 </ul> 11 </div> 12 <!--/.well --> 13 </div> 14 <!--/span--> 15 <div class="span10"> 16 @RenderBody() 17 </div> 18 <!--/span--> 19 </div> 20 <!--/row--> 21 22 <hr> 23 24 <footer> 25 <p>©xxxxx</p> 26 </footer> 27 28 </div>
但是在引用的时候有一个地方是要注意一下的,那就是引用问题,在view端目前是引用不了@Html.MenuLink的,我们需要再引用的页面@using Namespace;
我个人是不喜欢这么处理的,所以用另外一种方法来引用,在Views目录下有个web.config文件
1 <system.web.webPages.razor> 2 <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 3 <pages pageBaseType="System.Web.Mvc.WebViewPage"> 4 <namespaces> 5 <add namespace="System.Web.Mvc" /> 6 <add namespace="System.Web.Mvc.Ajax" /> 7 <add namespace="System.Web.Mvc.Html" /> 8 <add namespace="System.Web.Optimization"/> 9 <add namespace="System.Web.Routing" /> 10 <add namespace="Kendo.Mvc.UI" /> 11 </namespaces> 12 </pages> 13 </system.web.webPages.razor>