Расширение Html.ActionLink
Я пытаюсь расширить Html.ActionLink, поскольку я хочу добавить пользовательские метаданные для общего компонента (в данном случае модальный).
Моя цель заключается в дальнейшем расширении класса LinkExtensions в.Net MVC, который добавит значение в атрибут класса html и добавит пользовательский атрибут данных, что приведет к следующему:
<a href="/Controller/Action/id" class="show-in-modal style1 style2" data-title="Modal title">Link</a>
Помощник будет похож на метод MVC:
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 имеет перегрузки, которые принимают атрибуты HTML в форме IDictionary<string, object>
Есть ли метод расширения, который преобразует анонимные типы в изменяемый словарь?
Все, что я получаю при поиске, это как использовать метод Html.ActionLink().
2 ответа
Функция, которую вы ищете:
HtmlHelper.AnonymousObjectToHtmlAttributes ()
Вот одна из версий расширения ModalLink:
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);
}
Некоторое время назад я создал вспомогательный класс именно для такой ситуации. Это базовая урезанная версия. Я оставил комментарии XML для одного из методов, потому что это немного сбивает с толку.
HtmlAttributes.cs
/// <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;
}
}
В вашем случае, внутри вашего вспомогательного метода, вы бы сделали это:
HtmlAttributes finalAttributes = new HtmlAttributes(htmlAttributes);
finalAttributes.Add("data_title", "title");
finalAttributes.AddCssClass("show-in-modal");
Обратите внимание, что вы можете добавить (или удалить) их вместе по массе, если это необходимо:
finalAttributes.Add(new { data_title = "title", id = "id", data_extra = "extra" });
Затем вы можете просто передать finalAttributes как обычно, так как он расширяется Dictionary<string, object>
,
Это также полезно, когда вы создаете свои собственные средства визуализации элементов управления HTML, так как вы можете использовать attributes.ToMvcHtmlString()
метод для рендеринга атрибутов в HTML.