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

后端 未结 6 898
隐瞒了意图╮
隐瞒了意图╮ 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("{1}", url, text);
            }
    
        }
    

    then you can use it like this:

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

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

    
          
             
                
                
            
          
        
    

提交回复
热议问题