首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Html.ActionLink扩展

Html.ActionLink扩展
EN

Stack Overflow用户
提问于 2015-05-26 15:02:06
回答 2查看 2.3K关注 0票数 5

我试图扩展Html.ActionLink,因为我想为共享组件添加一个自定义元数据(在本例中是一个模式)。

我的目标是在LinkExtensions MVC中进一步扩展.Net类,它将向html类属性添加一个值,并添加一个自定义数据属性,结果如下:

代码语言:javascript
复制
<a href="/Controller/Action/id" class="show-in-modal style1 style2" data-title="Modal title">Link</a>

该助手看起来类似于MVC方法:

代码语言:javascript
复制
public static MvcHtmlString ModalLink(this HtmlHelper htmlHelper, string title, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    // Add 'show-in-modal' class here
    // Add 'data-title' attribute here

    return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}

@Html.ModalLink("Modal title", "Link", "action", "controller", new { id = "id" }, new { @class = "style1 style2" });

我面临的问题是,我无法轻松地修改htmlAttributes对象以添加类名和数据属性,这是有意义的,因为这是一个只读匿名对象。

有没有一种方法可以轻松地应用所需的值/元数据,而不必用反射将所有东西撕碎,然后重新组合在一起?

我注意到MVC有过载,它以IDictionary<string, object>的形式接受html属性,是否有一种扩展方法将匿名类型转换为可修改的字典?

在搜索过程中,我所得到的就是如何使用Html.ActionLink()方法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-26 16:17:13

您要寻找的功能是:

HtmlHelper.AnonymousObjectToHtmlAttributes()

https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.anonymousobjecttohtmlattributes(v=vs.118).aspx

下面是ModalLink扩展的一个版本:

代码语言:javascript
复制
public static MvcHtmlString ModalLink(this HtmlHelper htmlHelper, string title, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
  // Add 'show-in-modal' class here
  // Add 'data-title' attribute here

  var htmlAttr = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

  const string classKey = "class";
  const string titleKey = "data-title";

  const string classToAdd = "show-in-modal";
  if (htmlAttr.ContainsKey(classKey) == true)
  {
    htmlAttr[classKey] += " " + classToAdd;
  }
  else
  {
    htmlAttr.Add(classKey, classToAdd);
  }

  if (htmlAttr.ContainsKey(titleKey) == true)
  {
    htmlAttr[titleKey] = title;
  }
  else
  {
    htmlAttr.Add(titleKey, title);
  }

  return htmlHelper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(routeValues), htmlAttr);
}
票数 2
EN

Stack Overflow用户

发布于 2015-05-27 15:05:43

不久前,我为这种情况创建了一个帮助类。这是一个基本的缩减版本。我将XML注释放在其中一个方法中,因为否则它会有点混乱。

HtmlAttributes.cs

代码语言:javascript
复制
/// <copyright file="HtmlAttributes.cs"><author username="Octopoid">Chris Bellini</author></copyright>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.Mvc;

public class HtmlAttributes : Dictionary<string, object>
{
    public HtmlAttributes()
        : base()
    {
    }

    public HtmlAttributes(object anonymousAttributes)
        : base(HtmlHelper.AnonymousObjectToHtmlAttributes(anonymousAttributes))
    {
    }

    public HtmlAttributes(IDictionary<string, object> attributes)
        : base(attributes)
    {
    }

    public void Add(object anonymousAttributes)
    {
        this.Add(HtmlHelper.AnonymousObjectToHtmlAttributes(anonymousAttributes));
    }

    public void Add(IDictionary<string, object> attributes)
    {
        foreach (var attribute in attributes)
        {
            this.Add(attribute.Key, attribute.Value);
        }
    }

    public void AddCssClass(string cssClass)
    {
        if (cssClass == null) { throw new ArgumentNullException("cssClass"); }

        string key = "class";
        if (this.ContainsKey(key))
        {
            string currentValue;
            if (this.TryGetString(key, out currentValue))
            {
                this[key] = currentValue + " " + cssClass;
                return;
            }
        }

        this[key] = cssClass;
    }

    public void Remove(object anonymousAttributes)
    {
        this.Remove(HtmlHelper.AnonymousObjectToHtmlAttributes(anonymousAttributes));
    }

    /// <summary>
    /// Removes the value with the specified key from the <see cref="System.Collections.Generic.Dictionary<TKey,TValue>"/>.
    /// This method hides the base implementation, then calls it explicity.
    /// This is required to prevent the this.Remove(object) method catching base.Remove(string) calls.
    /// </summary>
    /// <param name="key">The key of the element to remove.</param>
    /// <returns>
    /// true if the element is successfully found and removed; otherwise, false.
    /// This method returns false if key is not found in the System.Collections.Generic.Dictionary<TKey,TValue>.
    /// </returns>
    /// <exception cref="System.ArgumentNullException">key is null.</exception>
    public new bool Remove(string key)
    {
        return base.Remove(key);
    }

    public void Remove(IDictionary<string, object> attributes)
    {
        foreach (var attribute in attributes)
        {
            this.Remove(attribute.Key);
        }
    }

    public MvcHtmlString ToMvcHtmlString()
    {
        return new MvcHtmlString(this.ToString());
    }

    public override string ToString()
    {
        StringBuilder output = new StringBuilder();

        foreach (var item in this)
        {
            output.Append(string.Format("{0}=\"{1}\" ", item.Key.Replace('_', '-'), item.Value.ToString()));
        }

        return output.ToString().Trim();
    }

    public bool TryGetString(string key, out string value)
    {
        object obj;
        if (this.TryGetValue(key, out obj))
        {
            value = obj.ToString();
            return true;
        }
        value = default(string);
        return false;
    }
}

在您的例子中,在helper方法中,您可以这样做:

代码语言:javascript
复制
HtmlAttributes finalAttributes = new HtmlAttributes(htmlAttributes);
finalAttributes.Add("data_title", "title");
finalAttributes.AddCssClass("show-in-modal");

注意,如果需要的话,您也可以大量添加(或删除)它们:

代码语言:javascript
复制
finalAttributes.Add(new { data_title = "title", id = "id", data_extra = "extra" });

然后,您可以像往常一样传入finalAttributes,因为它扩展了Dictionary<string, object>

当您制作自己的自定义HTML控件呈现器时,这也很有用,因为您可以使用attributes.ToMvcHtmlString()方法将属性呈现为HTML。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30462243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档