Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know if is possible to create a MVC Html Helper (like Html.BeginForm()) using only cshtml file(s)

I would like that helper was called like this :

@using(var myPanel = Helper.BeginMyPanel("myPanelXX")){
    using (var header = myPanel.BeginHeader())
    {
        <h3>My Panel header</h3>
    }

    using (var body = myPanel.BeginBody())
    {
        <p>One fine body…</p>
    }
}


Generating this :

HTML
<div class="myPanel" id="myPanelXX">
	<div class="myPanel-header">
		<h3>My Panel header</h3>
	</div>
	<div class="myPanel-body">
		<p>One fine body…</p>
	</div>
</div>
Posted
Updated 15-Apr-15 1:47am
v2

1 solution

I have implemented a solution in codebehind but I still haven't a solution just using cshtml files :



C#
using System;
using System.IO;
using System.Web.Mvc;

namespace SGO.Web
{
    public class SGOBlock : IDisposable
    {
        private HtmlHelper _helper;

        public SGOBlock(HtmlHelper helper, string title, string id)
        {
            _helper = helper;
            _helper.ViewContext.Writer.Write("<div id="\""" class="\"panel" mode="hold" />        }

        public void Dispose()
        {
            _helper.ViewContext.Writer.Write("");
        }

        public SGOBlockSection BeginSection(BlockSectionType type)
        {
            return new SGOBlockSection(_helper.ViewContext.Writer, type);
        }
    }

    #region SECTION

    public enum BlockSectionType
    {
        Header,
        Body,
        Footer
    }

    public class SGOBlockSection : IDisposable
    {
        private TextWriter _textWriter;
        private BlockSectionType _type;

        #region HTML
        private string htmlStartHeader = "<div class="\"panel-heading\""><div class="\"row\""><div class="\"col-lg-11\"">";
        private string htmlStartBody = "<div class="\"panel-body\"">";
        private string htmlStartFooter = "<div class="\"panel-footer\"">";

        private string htmlEndHeader = "</div><div class="\"col-md-1\""><a href="\"#\"">class="\">        private string htmlEndBody = "</a></div>";
        private string htmlEndFooter = "</div>";
        #endregion

        public SGOBlockSection(TextWriter textWriter, BlockSectionType type)
        {
            _type = type;
            _textWriter = textWriter;
            switch (_type)
            {
                case BlockSectionType.Header: _textWriter.Write(htmlStartHeader); break;
                case BlockSectionType.Body: _textWriter.Write(htmlStartHeader); break;
                case BlockSectionType.Footer: _textWriter.Write(htmlStartFooter); break;
            }
        }

        public void Dispose()
        {
            switch (_type)
            {
                case BlockSectionType.Header: _textWriter.Write(htmlEndHeader); break;
                case BlockSectionType.Body: _textWriter.Write(htmlEndHeader); break;
                case BlockSectionType.Footer: _textWriter.Write(htmlEndFooter); break;
            }
        }
    }

    #endregion
}

</div></div></div>


C#
using System.Web.Mvc;
namespace SGO.Web
{
    public static class SGOHelper
    {
        public static SGOBlock BeginSGOBlock(this HtmlHelper self, string id, string title)
        {
            return new SGOBlock(self, title, id);
        }
    }
}


@using (var block = Html.BeginSGOBlock("aa", "aaa"))
{
    using (var header = block.BeginSection(BlockSectionType.Header)){
     <small>header</small>
    }

    using (var header = block.BeginSection(BlockSectionType.Body))
    {
        <small>body</small>
    }

    using (var header = block.BeginSection(BlockSectionType.Footer))
    {
        <small>footer</small>
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900