Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 667785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:57:59+00:00 2026-05-13T23:57:59+00:00

I wonder if it’s possible to create an extension method which has functionality &

  • 0

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, and I could specificy its contents inside <% { & } %> tags.

For example, I could have a view like:

<% using(Html.BeginDiv("divId")) %>
<% { %>
    <!-- Form content goes here -->
<% } %>

This capability would be very useful in the context of the functionality I’m trying to produce with the example in this question

This would give me the ability to create containers for the types that I’ll be

<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>

<% using(Html.BeginDiv<MyType>(myType, tag) %>
<% { %>
    <!-- controls used for the configuration of MyType  -->
    <!-- represented in the context of a HTML element, e.g.:  -->

    <div class="MyType" prop1="123" prop2="234">
        <!-- add a select here -->
        <!-- add a radio control here -->
        <!-- whatever, it represents elements in the context of their type -->
    </div>

<% } %>

I realise this will produce invalid XHTML, but I think there could be other benefits that outweigh this, especially since this project doesn’t require that the XHTML validate to the W3C standards.

Thanks

Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:58:00+00:00Added an answer on May 13, 2026 at 11:58 pm

    Not quite sure how much value this has over simply defining a <div> element, but something like so

    /// <summary>
    /// Represents a HTML div in an Mvc View
    /// </summary>
    public class MvcDiv : IDisposable
    {
        private bool _disposed;
        private readonly ViewContext _viewContext;
        private readonly TextWriter _writer;
    
        /// <summary>
        /// Initializes a new instance of the <see cref="MvcDiv"/> class.
        /// </summary>
        /// <param name="viewContext">The view context.</param>
        public MvcDiv(ViewContext viewContext) {
            if (viewContext == null) {
                throw new ArgumentNullException("viewContext");
            }
            _viewContext = viewContext;
            _writer = viewContext.Writer;
        }
    
        /// <summary>
        /// Performs application-defined tasks associated with 
        /// freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }
    
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both 
        /// managed and unmanaged resources; <c>false</c> 
        /// to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
                _writer.Write("</div>");
            }
        }
    
        /// <summary>
        /// Ends the div.
        /// </summary>
        public void EndDiv()
        {
            Dispose(true);
        }
    }
    
    
    /// <summary>
    /// HtmlHelper Extension methods for building a div
    /// </summary>
    public static class DivExtensions
    {
        /// <summary>
        /// Begins the div.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns></returns>
        public static MvcDiv BeginDiv(this HtmlHelper htmlHelper)
        {
            // generates <div> ... </div>>
            return DivHelper(htmlHelper, null);
        }
    
        /// <summary>
        /// Begins the div.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static MvcDiv BeginDiv(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
        {
            // generates <div> ... </div>>
            return DivHelper(htmlHelper, htmlAttributes);
        }
    
        /// <summary>
        /// Ends the div.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        public static void EndDiv(this HtmlHelper htmlHelper)
        {
            htmlHelper.ViewContext.Writer.Write("</div>");
        }
    
        /// <summary>
        /// Helps build a html div element
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        private static MvcDiv DivHelper(this HtmlHelper htmlHelper, IDictionary<string, object> 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<string, object>{{"class","stripey"}}))
    { %>
           <p>Content Here</p>
    <% } %>
    

    will render

    <div class="stripey">
        <p>Content Here</p>
    </div>
    

    or without html attributes

    <% using (Html.BeginDiv())
    { %>
           <p>Content Here</p>
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should define comments = generic.GenericRelation(Comment) on the Post, to… May 14, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer The most common way I've seen is different .js language… May 14, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer I think you will make life quite difficult for yourself… May 14, 2026 at 10:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.