MVC 3 htmlhelper extension method to wrap around content

前端 未结 3 972
南旧
南旧 2020-12-30 01:53

I searched but could not find any quick solutions for an MVC 3 htmlhelper to create a wrapper method. What I\'m looking for is something like:

@html.createLi         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 02:46

    The way that this is done with BeginForm is that the return type MvcForm impliments IDisposable so that when used within a using statement, the Dispose method of MvcForm writes out the closing tag.

    You can write an extension method that does exactly the same thing.

    Here's one I just wrote to demonstrate.

    First off, the extension method:

    public static class ExtensionTest
    {
        public static MvcAnchor BeginLink(this HtmlHelper htmlHelper)
        {
            var tagBuilder = new TagBuilder("a");
            htmlHelper.ViewContext.Writer
                            .Write(tagBuilder.ToString(
                                                 TagRenderMode.StartTag));
            return new MvcAnchor(htmlHelper.ViewContext);
        }
    }
    

    And here's our new type, MvcAnchor:

    public class MvcAnchor : IDisposable
    {
        private readonly TextWriter _writer;
        public MvcAnchor(ViewContext viewContext)
        {
            _writer = viewContext.Writer;
        }
    
        public void Dispose()
        {
            this._writer.Write("");
        }
    }
    

    In your views you can now do:

    @{
        using (Html.BeginLink())
        { 
            @Html.Raw("Hello World")
        }
    }
    

    Which yields the result:

    Hello World
    

    Expanding this slightly to deal with your exact requirement:

    public static MvcAnchor BeginLink(this HtmlHelper htmlHelper, 
                                       string href, 
                                       string title)
    {
        var tagBuilder = new TagBuilder("a");
        tagBuilder.Attributes.Add("href",href);
        tagBuilder.Attributes.Add("title", title);
        htmlHelper.ViewContext.Writer.Write(tagBuilder
                                        .ToString(TagRenderMode.StartTag));
        return new MvcAnchor(htmlHelper.ViewContext);
    }
    

    and our view:

    @{
      using (Html.BeginLink("http://stackoverflow.com", "The Worlds Best Q&A site"))
      { 
          @Html.Raw("StackOverflow - Because we really do care")
      }
    }
    

    which yields the result:

    
       StackOverflow - Because we really do care
    

提交回复
热议问题