本文介绍了像Html.BeginForm()的MVC Html Helper使用cshtml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以仅使用cshtml文件创建MVC Html Helper(如 Html.BeginForm()



我希望这个帮手像这样调用:



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 :

<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>

推荐答案

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>







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>
    }
}


这篇关于像Html.BeginForm()的MVC Html Helper使用cshtml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:05