Create Extension Method to Produce Open & Closing Tags like Html.BeginForm()

前端 未结 1 983
遥遥无期
遥遥无期 2020-12-30 10:07

I wonder if it\'s possible to create an extension method which has functionality & behaviour similar to Html.BeginForm(), in that it would generate a complete Html tag,

1条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 10:24

    Not quite sure how much value this has over simply defining a

    element, but something like so

    /// 
    /// Represents a HTML div in an Mvc View
    /// 
    public class MvcDiv : IDisposable
    {
        private bool _disposed;
        private readonly ViewContext _viewContext;
        private readonly TextWriter _writer;
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The view context.
        public MvcDiv(ViewContext viewContext) {
            if (viewContext == null) {
                throw new ArgumentNullException("viewContext");
            }
            _viewContext = viewContext;
            _writer = viewContext.Writer;
        }
    
        /// 
        /// Performs application-defined tasks associated with 
        /// freeing, releasing, or resetting unmanaged resources.
        /// 
        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }
    
        /// 
        /// Releases unmanaged and - optionally - managed resources
        /// 
        /// true to release both 
        /// managed and unmanaged resources; false 
        /// to release only unmanaged resources.
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                _writer.Write("
    "); } } /// /// Ends the div. /// public void EndDiv() { Dispose(true); } } /// /// HtmlHelper Extension methods for building a div /// public static class DivExtensions { /// /// Begins the div. /// /// The HTML helper. /// public static MvcDiv BeginDiv(this HtmlHelper htmlHelper) { // generates
    ...
    > return DivHelper(htmlHelper, null); } /// /// Begins the div. /// /// The HTML helper. /// The HTML attributes. /// public static MvcDiv BeginDiv(this HtmlHelper htmlHelper, IDictionary htmlAttributes) { // generates
    ...
    > return DivHelper(htmlHelper, htmlAttributes); } /// /// Ends the div. /// /// The HTML helper. public static void EndDiv(this HtmlHelper htmlHelper) { htmlHelper.ViewContext.Writer.Write("
    "); } /// /// Helps build a html div element /// /// The HTML helper. /// The HTML attributes. /// private static MvcDiv DivHelper(this HtmlHelper htmlHelper, IDictionary htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("div"); tagBuilder.MergeAttributes(htmlAttributes); htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag)); MvcDiv div = new MvcDiv(htmlHelper.ViewContext); return div; } }

    and use like so

    <% using (Html.BeginDiv(new Dictionary{{"class","stripey"}}))
    { %>
           

    Content Here

    <% } %>

    will render

    Content Here

    or without html attributes

    <% using (Html.BeginDiv())
    { %>
           

    Content Here

    <% } %>

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