2012-01-19 15 views
16

以同樣的方式,我可以在ASP.NET MVC中創建一個指向控制器中的動作的ActionLink(例如 - @Html.ActionLink("MyDisplayText", "MyAction", "MyController")),我希望能夠創建一個帶有明確定義的外部URL的超鏈接。HtmlHelper如何用於創建外部超鏈接?

我正在尋找的是像@Html.HyperLink("stackoverflow", "http://www.stackoverflow.com/")一些代碼,生成此HTML:<a href="http://www.stackoverflow.com/">stackoverflow</a>

如果這是不可能的,我永遠可以手工編寫的HTML。

(這是我的第一個計算器問題。多麼令人興奮。)

+0

歡迎登機,請不要忘記上/下投票並在適當的時候標記答案。如果你接受的答案比例很高,你會發現你會得到更好的答案。 – SventoryMang

+0

謝謝!向上/向下投票需要15個聲望,所以我現在還不能這樣做,但我會牢記這一點。 –

回答

16

定製的助手看起來是這樣的:

namespace System.Web.Mvc { 
    public static class HtmlHelperExtensions { 
     public static MvcHtmlString Hyperlink(this HtmlHelper helper, string url, string linkText) { 
      return MvcHtmlString.Create(String.Format("<a href='{0}'>{1}</a>", url, linkText)); 
     } 
    } 
} 

五月這是您使用的許多自定義HtmlHelpers中的第一個!

+1

謝謝!你的例子產生HTML:'< stackoverflow.com ' >堆棧溢出</a >' –

+0

我將返回類型更改爲MvcHtmlString並且它工作得很好。 –

+0

啊,沒錯。對不起,忘了。我編輯了我的答案。 – jkokorian

1
public static class HtmlHelpers  
{ 
    public static string Hyperlink(this HtmlHelper helper, string href, string text) 
    { 
     String.Format("<a href=\"{0}\">{1}</a>", href, text); 
    } 
} 

會工作。在HtmlHelper中使用這個表示擴展方法。此外,如果你想成爲超爽的MVC十歲上下的風格,你可以使用TagBuilder甚至電源選項,如目標:

public static MvcHtmlString Script(this HtmlHelper helper, string href, string text, bool openInNewWindow = false) 
    { 
     var builder = new TagBuilder("a"); 
     builder.MergeAttribute("href", href); 
     if(openInNewWindow) 
     { 
      builder.MergeAttributes("target", "_blank"); 
     } 
     builder.SetInnerText(text); 
     return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal)); 
    } 
+0

擴展方法需要是靜態的,它們必須在靜態類中聲明 –

+0

謝謝,內存不足。 – SventoryMang

+0

感謝您的幫助。 –

0

你需要一個HTML輔助擴展

 

public static class HtmlHelpers 
{ 
    public static string HyperLink(this HtmlHelper html, string text, string href) 
    { 
    return string.Format(@"<a href="{0}">{1}</a>", href, text); 
    } 
} 
 

13

這個問題是幾年前的,並旨在爲ASP.NET MVC V2的答案。現在可能有更好,更好的方法來做到這一點,我強烈建議你考慮@ jkokorian的answer。這只是一個很好的方式來顯示你可以做什麼,而不是你應該做!

沒有什麼可怕的新的補充,但我傾向於使用object對HTML輔助可選參數,可以添加new RouteValueDictionary(obj)接着轉成你可以用MergeAttributes添加KVP。

代碼:

基本構造:

@Html.ExternalLink("http://www.example.com", "Example!") 

的HTML屬性:鑑於

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)); 
    } 
} 

使用

@Html.ExternalLink("http://www.example.com", "Example!", new { title = "Example" }) 

隨着HTML和數據屬性:

@Html.ExternalLink("http://www.example.com", "Example!", new { target = "_blank" }, new { id = 1 }) 

單元測試:

[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>"); 
} 
0

我不能得到上述解決方案的工作,並做了一些簡單得多。

控制器

Contracts model = db.Contract 
ViewBag.Link = "<a href='" + model.Link + "'>View Link</a>"; 

VIEW

1

老問題:但答案很簡單 - 不知道這是否是永遠的解決方案。

@Html.RouteLink("External Link", new {}, new { href="http://www.google.com" }) 

很好地訣竅 - 雖然可能有點矯枉過正。