开发者

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

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 works:

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.Encode(item.Id) %>
        </td>
        <td>
            <%= String.Format("<a href=\"{0}\">link</a>", item.Url) %>
        </td>
    </tr>

<% } %>

But I am wondering if it's really the right approach. Am I missing some obvious MVC control h开发者_StackOverflow中文版ere?


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


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>


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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜