Asp MVC 4 creating custom html helper method similar to Html.BeginForm

Error processing SSI file


  1. Bruno

    • 2021/3/15

    You can't return MvcHtmlString. Instead of that you should write html to the writer and return class that implement IDisposable and during invoking Dispose will write closing part of your HTML.

    public static class BeginViewHelper
        public static IDisposable BeginView(this HtmlHelper helper, string viewId)
            helper.ViewContext.Writer.Write(string.Format("<div id='{0}'>", viewId));
            return new MyView(helper);
        class MyView : IDisposable
            private HtmlHelper helper;
            public MyView(HtmlHelper helper)
                this.helper = helper;
            public void Dispose()

    If you have more complex structure you can try to use TagBuilder:

    TagBuilder tb = new TagBuilder("div");
  2. Seven

    • 2019/1/12

    You can't return MvcHtmlString. Instead of that you should write html to the writer and return class that implement IDisposable and during 

  3. Marino

    • 2020/12/17

    I want to make a custom html helper and use it like this (similar to Html.BeginForm) public static class BeginViewHelper { public static MvcHtmlString BeginView (this HtmlHelper helper, string viewId) { var parentDiv = new TagBuilder ("div"); parentDiv.MergeAttribute ("data-bind", "preventBinding: true"); return new MvcHtmlString (); } } I read how to make basic html helper but the examples I saw does not give me information how to make it in my case.

  4. Kash

    • 2016/4/5

    Asp MVC 4 creating custom html helper method similar to Html.BeginForm. Asked 2 Weeks ago Answers: 5 Viewed 3 times. I have the following html:

  5. Melvin

    • 2015/1/31

    The BeginForm method of mvc returns a IDisposable instance of the MvcForm class. If you look inside the mvc code on codeplex, you can check how the mvc team has developed this feature.

    Take a look at theses links:

    MvcForm class (IDisposable)

    Forms Extensions (for html helper)

  6. Garcia

    • 2015/9/27

    If you want to create HTML Helpers that work just like the standard HTML Helpers included in the ASP.NET MVC framework then you need to create extension methods. Extension methods enable you to add new methods to an existing class. When creating an HTML Helper method, you add new methods to the HtmlHelper class represented by a view's Html property.

  7. Bo

    • 2020/2/18

    If you've used ASP.NET MVC for more than five minutes, you've probably used the Html.BeginForm helper method. This method wraps your 

  8. Jamari

    • 2016/6/12

    There are two ways in MVC to create custom Html helpers as below. Adding extension method for HtmlHelper class; Using static method; Adding extension method for HtmlHelper class We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Example

  9. Jonathan

    • 2020/1/3

    Slawek has the correct answer, but I thought I would add to it with my experience.

    I wanted to create a helper to display widgets on a page (almost like jQuery's widgets with the title bar and a content portion). Something to the effect of:

    @using (Html.BeginWidget("Widget Title", 3 /* columnWidth */))
        @* Widget Contents *@

    The MVC source uses something similar to what Slawek posted, but I feel like placing the start tag in the helper and end tag in the actual object wasn't "tidy", nor did it keep concerns in the corect place. Should I want to change the appearance, I'm now doing so in two places instead of what I felt was one logical place. So I came up with the following:

    /// <summary>
    /// Widget container
    /// </summary>
    /// <remarks>
    /// We make it IDIsposable so we can use it like Html.BeginForm and when the @using(){} block has ended,
    /// the end of the widget's content is output.
    /// </remarks>
    public class HtmlWidget : IDisposable
        #region CTor
        // store some references for ease of use
        private readonly ViewContext viewContext;
        private readonly System.IO.TextWriter textWriter;
        /// <summary>
        /// Initialize the box by passing it the view context (so we can
        /// reference the stream writer) Then call the BeginWidget method
        /// to begin the output of the widget
        /// </summary>
        /// <param name="viewContext">Reference to the viewcontext</param>
        /// <param name="title">Title of the widget</param>
        /// <param name="columnWidth">Width of the widget (column layout)</param>
        public HtmlWidget(ViewContext viewContext, String title, Int32 columnWidth = 6)
            if (viewContext == null)
                throw new ArgumentNullException("viewContext");
            if (String.IsNullOrWhiteSpace(title))
                throw new ArgumentNullException("title");
            if (columnWidth < 1 || columnWidth > 12)
                throw new ArgumentOutOfRangeException("columnWidth", "Value must be from 1-12");
            this.viewContext = viewContext;
            this.textWriter = this.viewContext.Writer;
            this.BeginWidget(title, columnWidth);
        #region Widget rendering
        /// <summary>
        /// Outputs the opening HTML for the widget
        /// </summary>
        /// <param name="title">Title of the widget</param>
        /// <param name="columnWidth">Widget width (columns layout)</param>
        protected virtual void BeginWidget(String title, Int32 columnWidth)
            title = HttpUtility.HtmlDecode(title);
            var html = new System.Text.StringBuilder();
            html.AppendFormat("<div class=\"box grid_{0}\">", columnWidth).AppendLine();
            html.AppendFormat("<div class=\"box-head\">{0}</div>", title).AppendLine();
            html.Append("<div class=\"box-content\">").AppendLine();
        /// <summary>
        /// Outputs the closing HTML for the widget
        /// </summary>
        protected virtual void EndWidget()
        #region IDisposable
        private Boolean isDisposed;
        public void Dispose()
        public virtual void Dispose(Boolean disposing)
            if (!this.isDisposed)
                this.isDisposed = true;

    Then, that makes our helper a little more clear (and no UI code in two places):

    public static HtmlWidget BeginWidget(this HtmlHelper htmlHelper, String title, Int32 columnWidth = 12)
      return new HtmlWidget(htmlHelper.ViewContext, title, columnWidth);

    Then we can use it as I've done at the top of this post.

  10. Gatti

    • 2020/3/14

    Next, I describe two methods of creating custom HTML Helpers: I explain how using (Html.BeginForm()) { %> <label for="firstName">First 

  11. Hector

    • 2015/8/14

    One of it’s primary disadvantages, however, is that you have to write a lot of markup to get your layouts to work correctly. We can reduce this by creating a custom HTML Helper that works like Html.BeginForm(). @using (Html.BeginRow()) { // Grid content goes here } Step 1. Create a Private Class that implements IDisposable. Have the constructor take a TextWriter object. Set the dispose method to use the text writer to output the closing tag for the row.

  12. Jesiah

    • 2016/2/16

    This method is only available for razor view engine. To create an html helper, define the method using @helper property like below. @helper 

  13. Kylan

    • 2018/4/18

    ASP.NET MVC providesthree ways to define the custom html helper. Using the @helper. In this method we create a static class with static method, this method will return the HTML string. So, first create a folder with name “ Custom_Helpers ” in root directory.

  14. Morgan

    • 2021/2/2

    Introduction HTML Helper is just a method that returns the HTML content in a view. Developers can use HTML helpers to render standard HTML 

  15. Yisroel

    • 2016/9/30

    As we already discussed the HTML helper is a method that returns an HTML string. Then this HTML string is rendered in a view. ASP.NET MVC provides many built-in HTML helper methods that we can directly use in a view. The MVC framework also provides the facility to create Custom HTML Helpers in ASP.NET MVC application. Once you create your custom HTML helper method then you can reuse it many times.

  16. Kevin

    • 2017/1/12

    For example we use Html.TextBox() helper method it generates html input textbox. Write the following code snippet in MVC View: <%=Html.TextBox("txtName" 

  17. Nathanael

    • 2016/12/29

    ASP.NET MVC Custom Html Helper using Extension Method. If we want a custom Html Helper to be used just like standard Html helper, then available approach is to create an extension method on Html Helper class. Custom Html Helpers we create using Extension methods will be available to Html property of View. For the purpose of implementation, we will create a custom Html Helper i.e. “CustomImage” using extension method approach as follows:

  18. Israel

    • 2015/6/10

    Let's see some of the Standard HTML Helpers: Form: For creating the Form element, we can use BeginForm() and EndForm() extension method. The 

  19. Theodore

    • 2019/2/3

    In MVC, if we want to create a view it should contain HTML code for specifying the mark up. NET MVC framework comes with a set of HTML Helper methods.

  20. Adan

    • 2016/11/9

    This technique enables us to create templates for presenting and editing From the usage point of view, the mechanism is similar to Html helpers.

Comments are closed.

More Posts