博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc扩展HtmlHelper功能
阅读量:4668 次
发布时间:2019-06-09

本文共 5527 字,大约阅读时间需要 18 分钟。

HtmlHelper

 

 

 

解决:

  直接写HTML的话如果语句有语法错误,如缺少结尾标记</b>,编译器不会报错,出来的页面可能会很乱且难以查出错误在哪。如果用HtmlHelper在编译的时候就会指出错误,可以及时修改

  View中的页面一般都是动态页面,也就是说如果没有HtmlHelper,我们经常会写如<input type="text" value="@id">这样的服务器端代码和HTML代码的混合代码。这样写不仅形式混乱、执行顺序不好判断,而且出错也不容易发现,不如全部用HtmlHelper写成服务器端代码。而且用HtmlHelper做数据绑定更方便。

  HtmlHelper和HTML语言的关系,我觉得跟Linq和SQL语言的关系差不多。就是说微软给你提供了一种方式让你在不熟悉HTML或SQL这种非.NET语言的时候,使用.NET框架内的与之等价的类来帮助你更好的解决问题。

 

 

自定义HtmlHelperExtensions

namespace DVM.WebSite.Common{    ///     /// URL访问检查器    ///     public class UrlAccessChecker    {        ///         /// 检查当前用户是否具备访问特定Action权限        ///         /// Action名字        /// Controller名字        /// 
具备访问权限返回True,否则返回False
public static Boolean HasAccess(String actionName, String controllerName) { //......... return false; } } public static class HtmlHelperExtensions { public static MvcHtmlString DictDropDownListFor
(this HtmlHelper
htmlHelper, String category, Expression
> expression, string optionLabel=null, object htmlAttributes=null) where TModel:class { var dict = SystemDictionaryCache.Instance.GetNodesByRoot(category); var value = expression.Compile()(htmlHelper.ViewData.Model); var selectList = new SelectList(dict, "Key", "Value", value); return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes); } public static MvcHtmlString DictSubDropDownListFor
(this HtmlHelper
htmlHelper, String category,int pvalue,Expression
> expression, string optionLabel = null, object htmlAttributes = null) where TModel : class { var dict = pvalue<=0?new Dictionary
():SystemDictionaryCache.Instance.GetSubNodes(category,pvalue); var value = expression.Compile()(htmlHelper.ViewData.Model); var selectList = new SelectList(dict, "Key", "Value", value); return htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes); } ///
/// 生成对象的Json字符串 /// ///
helper ///
目标对象 ///
Html字符串
public static MvcHtmlString MvcJson(this HtmlHelper helper, object o) { JavaScriptSerializer serializer = new JavaScriptSerializer(); StringBuilder builder = new StringBuilder(); serializer.Serialize( o, builder); return new MvcHtmlString(builder.ToString()); } ///
/// 创建指定的a元素,如果用户没有权限访问,则返回空串 /// ///
htmlHelper ///
链接文本 ///
Action名称 ///
HTML内容
public static MvcHtmlString ActionLinkACL( this HtmlHelper htmlHelper, string linkText, string actionName ) { var result = new MvcHtmlString(String.Empty); var controllerName = htmlHelper.ViewContext.RouteData.Values["Controller"].ToString(); if (UrlAccessChecker.HasAccess(actionName, controllerName)) { return htmlHelper.ActionLink(linkText, actionName, controllerName); } return result; } ///
/// 创建指定的a元素,如果用户没有权限访问,则返回空串 /// ///
htmlHelper ///
链接文本 ///
Action名称 ///
路由参数 ///
Html内容
public static MvcHtmlString ActionLinkACL( this HtmlHelper htmlHelper, string linkText, string actionName, Object routeValues ) { var result = new MvcHtmlString(String.Empty); var controllerName = htmlHelper.ViewContext.RouteData.Values["Controller"].ToString(); if (UrlAccessChecker.HasAccess(actionName, controllerName)) { return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues); } return result; } ///
/// 创建AjaxLink,默认Update对象为#wrap /// 默认替代模式为replace,默认请求方式为Get /// ///
htmlHelper ///
链接文本 ///
Area和Controller名称,含area则使用/链接 ///
html属性列表 ///
内部Html ///
Html内容
public static MvcHtmlString AjaxLink2ACL( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, IDictionary
htmlAttributes, string innerHtml, string method = "GET" ) { TagBuilder tb = new TagBuilder("a"); var defaultAttributes = new Dictionary
{ { "data-ajax-update","#wrap" }, { "data-ajax-mode","replace" }, { "data-ajax-method","GET" }, { "data-ajax-complete","closeWaitingDialog" }, { "data-ajax-begin","waitingDialog" }, { "data-ajax-success","onRequestSuccess" }, { "data-ajax-failure","onRequestFailed" }, { "data-ajax","true" }, }; if (!String.IsNullOrEmpty(method)) { defaultAttributes["data-ajax-method"] = method; } if (htmlAttributes != null) { foreach (var kp in htmlAttributes) { defaultAttributes[kp.Key] = kp.Value; } } foreach (var attr in defaultAttributes) { tb.MergeAttribute(attr.Key, Convert.ToString(attr.Value)); } var linkUrl = String.Format("{0}/{1}/{2}",HttpRuntime.AppDomainAppVirtualPath,controllerName,actionName); linkUrl = linkUrl.Replace("//", "/"); tb.MergeAttribute("href", linkUrl); tb.InnerHtml = innerHtml+linkText; return new MvcHtmlString(tb.ToString(TagRenderMode.Normal)); } }}

  

 

前台页面调用:

@using DVM.WebSite.Common;

  

  • @Html.AjaxLink2ACL(subMenu.Name, subMenu.ActionName, String.IsNullOrEmpty(subMenu.AreaName) ? subMenu.ControllerName : subMenu.AreaName + "/" + subMenu.ControllerName,new Dictionary < String, Object >{
    { "class","" }}, "")
    @*
    @Url.Action(subMenu.ControllerName, subMenu.ActionName)
    @Ajax.RouteLink(subMenu.Name, new { controller = String.IsNullOrEmpty(subMenu.AreaName) ? subMenu.ControllerName : subMenu.AreaName + "/" + subMenu.ControllerName, action = subMenu.ActionName }, ajaxOptions)
    *@
  •   

     

    转载于:https://www.cnblogs.com/panpanwelcome/p/7683164.html

    你可能感兴趣的文章
    jquery datatables
    查看>>
    php函数封装
    查看>>
    Cloudera是个什么东西
    查看>>
    Linux系统下安装 apache2.4的过程
    查看>>
    git cherry-pick 的使用
    查看>>
    UIScrollView的属性总结
    查看>>
    iOS提交审核:您的 App 正在使用广告标识符 (IDFA)
    查看>>
    AJAX原理及XMLHttpRequest对象分析
    查看>>
    在汇报工作时,我需要做些什么
    查看>>
    lxc 一些有用的资源
    查看>>
    c# 内部类使用接口IComparer实现排序
    查看>>
    spring boot 项目搭建时,各个依赖的作用
    查看>>
    SaltStack
    查看>>
    青铜器RDM全面支持CMMI、GJB5000A L2~L5认证评估
    查看>>
    关于串行通信、通信接口、接口连接器、通信协议的理解
    查看>>
    提高你的Java代码质量吧:不要让类型默默转换
    查看>>
    HTML5 audio标签 打造属于自己的音乐播放器
    查看>>
    【第二组】典型场景:用户上传自定义谜题,工作序号:002,2017/7/6
    查看>>
    ubuntu17.04 调试系统工具bcc,systamtap安装
    查看>>
    A Fast and Scalable Web Platform by Extending NGINX with Lua
    查看>>