Questa domanda è diversi anni ed è stato inteso come una risposta per ASP.NET MVC v2. Probabilmente ci sono modi migliori e migliori per farlo ora, e ti suggerisco caldamente di prendere in considerazione l'idea di @jkokorian's answer. Questo è solo un bel modo di mostrare cosa si può fare , non quello che si do do!
Niente di terribilmente nuovo da aggiungere, ma io tendo a usare object
per params facoltativi su aiutanti HTML, e aggiungere new RouteValueDictionary(obj)
che li trasforma in un KVP cui è possibile aggiungere con MergeAttributes
.
Codice:
public static class HtmlHelpers {
public static MvcHtmlString ExternalLink(this HtmlHelper htmlHelper, string url, object innerHtml, object htmlAttributes = null, object dataAttributes = null) {
var link = new TagBuilder("a");
link.MergeAttribute("href", url);
link.InnerHtml = innerHtml.ToString();
link.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
//Data attributes are definitely a nice to have.
//I don't know of a better way of rendering them using the RouteValueDictionary however.
if (dataAttributes != null) {
var values = new RouteValueDictionary(dataAttributes);
foreach (var value in values) {
link.MergeAttribute("data-" + value.Key, value.Value.ToString());
}
}
return MvcHtmlString.Create(link.ToString(TagRenderMode.Normal));
}
}
Utilizzo in vista:
costruttore di base:
@Html.ExternalLink("http://www.example.com", "Example!")
Con attributi HTML:
@Html.ExternalLink("http://www.example.com", "Example!", new { title = "Example" })
con HTML e dati di attributi:
@Html.ExternalLink("http://www.example.com", "Example!", new { target = "_blank" }, new { id = 1 })
Prove di unità:
[TestMethod]
public void ExternalLink_Example_ShouldBeValid() {
//Arrange
var url = "http://www.example.com";
var innerHtml = "Example";
//Act
var actual = HtmlHelpers.ExternalLink(null, url, innerHtml);
//Assert
actual.ToString().Should().Be(@"<a href=""http://www.example.com"">Example</a>");
}
[TestMethod]
public void ExternalLink_Example_WithHtmlAttributes_ShouldBeValid() {
//Arrange
var url = "http://www.example.com";
var innerHtml = "Example";
//Act
var actual = HtmlHelpers.ExternalLink(null, url, innerHtml, new { title = "Example!", @class = "myLink", rel = "external", target = "_blank" });
//Assert
actual.ToString().Should().Be(@"<a class=""myLink"" href=""http://www.example.com"" rel=""external"" target=""_blank"" title=""Example!"">Example</a>");
}
[TestMethod]
public void ExternalLink_Example_WithDataAttributes_ShouldBeValid() {
//Arrange
var url = "http://www.example.com";
var innerHtml = "Example";
//Act
var actual = HtmlHelpers.ExternalLink(null, url, innerHtml, new { title = "Example!" }, new { linkId = 1 });
//Assert
actual.ToString().Should().Be(@"<a data-linkId=""1"" href=""http://www.example.com"" title=""Example!"">Example</a>");
}
Benvenuto a bordo, per favore non dimenticare di votare su/giù e contrassegnare le risposte come appropriato. Scoprirai che otterrai risposte molto meglio se la tua percentuale di risposte accettate è elevata. – SventoryMang
Grazie! I voti Up/Down richiedono 15 reputazione, quindi non posso ancora farlo, ma lo terrò a mente. I metodi di estensione –