How to render plain HTML links in Asp.Net MVC loop?

后端 未结 6 885
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 07:45

I would like to render a list of HTML links in ASP.NET MVC. Note that the links are absolute and external to the website being designed. The following code

6条回答
  •  梦毁少年i
    2020-12-29 08:04

    Orchard project has an HtmlHelper extensions class which has a link builder method.

    See: HtmlHelperExtensions.Link()

    http://orchard.codeplex.com/SourceControl/changeset/view/dbec3d05e6d1#src%2fOrchard%2fMvc%2fHtml%2fHtmlHelperExtensions.cs

    Allows the following usage:

  • @Html.Link(Model.Path, Model.Title)
  • Update The above link is no longer valid, but if you download the source, you will find HtmlHelperExtensions which has 5 overloads for Links, one of which looks like this:

    public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) {
            var tagBuilder = new TagBuilder("a") { InnerHtml = htmlHelper.Encode(linkContents) };
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("href", href);
            return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        } 
    

提交回复
热议问题