加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 你如何覆盖Html.ActionLink?

发布时间:2020-12-16 03:39:46 所属栏目:asp.Net 来源:网络整理
导读:通常我会想要使用数据库中的内容呈现ActionLink(想象一下Id / Name组合),所以我将把以下内容放在我的视图中: @foreach (var row in Model) { li@Html.ActionLink(row.Name,"Action","Controller",new { id = row.Id })/li } 但是,如果Name是空字符串,则会引
通常我会想要使用数据库中的内容呈现ActionLink(想象一下Id / Name组合),所以我将把以下内容放在我的视图中:

@foreach (var row in Model)
        {
            <li>@Html.ActionLink(row.Name,"Action","Controller",new { id = row.Id })</li>
        }

但是,如果Name是空字符串,则会引发异常.有什么办法可以防止这种情况发生.我想覆盖ActionLink,但它是一个扩展,所以我无法覆盖.

有什么建议?

编辑:

首先,我不控制数据,或者我会确保名称字段是必需的并且始终填充.不幸的是,这种情况并非如此.

其次,我意识到用户没有任何东西可以点击,但我相信渲染空链接比给他们YSOD更好.

解决方法

事实证明,实例方法总是优先于具有相同签名的扩展方法.

我能够加载CustomHtmlHelper并将实例方法ActionLink方法放在新类中:

public abstract class CustomWebViewPage<T> : WebViewPage<T>
{
    public new CustomHtmlHelper<T> Html { get; set; }

    public override void InitHelpers()
    {
        Ajax = new AjaxHelper<T>(ViewContext,this);
        Url = new UrlHelper(ViewContext.RequestContext);

        //Load Custom Html Helper instead of Default
        Html = new CustomHtmlHelper<T>(ViewContext,this);
    }
}

并且HtmlHelper如下(从Reflector复制的ActionLink方法没有LinkText错误检查:

public class CustomHtmlHelper<T> : HtmlHelper<T>
{
    public CustomHtmlHelper(ViewContext viewContext,IViewDataContainer viewDataContainer) :
        base(viewContext,viewDataContainer)
    {
    }

    //Instance methods will always be called instead of extension methods when both exist with the same signature...

    public MvcHtmlString ActionLink(string linkText,string actionName)
    {
        return ActionLink(linkText,actionName,null,new RouteValueDictionary(),new RouteValueDictionary());
    }

    public MvcHtmlString ActionLink(string linkText,string actionName,object routeValues)
    {
        return ActionLink(linkText,new RouteValueDictionary(routeValues),string controllerName)
    {
        return ActionLink(linkText,controllerName,RouteValueDictionary routeValues)
    {
        return ActionLink(linkText,routeValues,object routeValues,object htmlAttributes)
    {
        return ActionLink(linkText,AnonymousObjectToHtmlAttributes(htmlAttributes));
    }

    public MvcHtmlString ActionLink(string linkText,RouteValueDictionary routeValues,IDictionary<string,object> htmlAttributes)
    {
        return ActionLink(linkText,htmlAttributes);
    }

    public MvcHtmlString ActionLink(string linkText,string controllerName,object> htmlAttributes)
    {
        return MvcHtmlString.Create(GenerateLink(ViewContext.RequestContext,RouteCollection,linkText,htmlAttributes));
    }

    public MvcHtmlString ActionLink(string linkText,string protocol,string hostName,string fragment,protocol,hostName,fragment,htmlAttributes));
    }
}

最后,在Views / Web.config文件中设置pageBaseType int以使用新的自定义WebViewPage:

<system.web.webPages.razor>
    ...
    <pages pageBaseType="Fully.Qualified.Namespace.CustomWebViewPage">
    ...
    </pages>
  </system.web.webPages.razor>

希望这有助于其他人.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读