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

后端 未结 6 797
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  • 2020-12-29 08:04

    You are not missing anything but good approach is to create extender method on HtmlHelper:

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

    then you can use it like this:

    <tr>
            <td>
                <%= Html.Encode(item.Id) %>
            </td>
            <td>
                <%= Html.SimpleLink(item.Url,item.Text) %>
            </td>
        </tr>
    

    [edit] I forgot to add. In order to use this HtmlHelper extender throughout application you need to add the following in the web config file:

    <system.web>
          <pages>
             <namespaces>
                <!-- leave rest as-is -->
                <add namespace="theNamespaceWhereHtmlHelpersClassIs"/>
            </namespaces>
          </pages>
        </system.web>
    
    0 讨论(0)
  • 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:

    <li>@Html.Link(Model.Path, Model.Title)</li> 
    

    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<string, object> htmlAttributes) {
            var tagBuilder = new TagBuilder("a") { InnerHtml = htmlHelper.Encode(linkContents) };
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("href", href);
            return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        } 
    
    0 讨论(0)
  • 2020-12-29 08:15

    I like to implement it the way the MVC framework does it, using the tag builder class. This way I can pass through the htmlAttributes parameter to add things like class or other attributes:

    public static MvcHtmlString HtmlLink(this HtmlHelper html, string url, string text, object htmlAttributes)
    {
     TagBuilder tb = new TagBuilder("a");
     tb.InnerHtml = text;
     tb.MergeAttributes(new RouteValueDictionary(htmlAttributes));
     tb.MergeAttribute("href", url);
     return MvcHtmlString.Create(tb.ToString(TagRenderMode.Normal));
    }
    

    May seem like overkill just to generate a link, but it means you don't have to muck about with string format patterns to insert additional HTML attributes on the link

    0 讨论(0)
  • 2020-12-29 08:16

    I think it's good. A simple foreach does the repeater role in MVC.

    0 讨论(0)
  • 2020-12-29 08:20

    I would rather use

    <td><a href="<%= item.Url %>">link</a></td>
    

    seems somewhat "cleaner" to me, but I think your approach just as good.

    0 讨论(0)
  • 2020-12-29 08:27

    To avoid Html encoding use @Html.Raw(url).

    0 讨论(0)
提交回复
热议问题