Helper for tag html a

佐手、 提交于 2020-01-04 04:57:29

问题


is there any helper in asp.net MVC3

<a href="www.google.com">Go to Google </a>

?

Not for an action but to a static link


回答1:


I don't believe there is, but I'm not sure why you would want one. You'd actually end up with more code:

<a href="http://www.google.com/">Go to Google</a>

<%: Html.Link("http://www.google.com/", "Go to Google") %>

@Html.Link("http://www.google.com/", "Go to Google")

Update: If you want to create a Link() helper like that above, you would use an extension method:

 public static class LinkExtensions
 {
    public static MvcHtmlString Link(this HtmlHelper helper, string href, string text)
    {
        var builder = new TagBuilder("a");
        builder.MergeAttribute("href", href);
        builder.SetInnerText(text);

        return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
    }
 }


来源:https://stackoverflow.com/questions/5400571/helper-for-tag-html-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!