简体   繁体   English

创建一个ASP MVC助手来包围HTML代码

[英]Creating an asp mvc helper to surround html code

I'm trying to create a custom helper that would generate collapsible panels with html content, and which would work like Html.BeginForm, being called like this : 我正在尝试创建一个自定义助手,该助手将生成具有html内容的可折叠面板,并且其工作方式类似于Html.BeginForm,如下所示:

@Html.BeginCollapsiblePanel("Test block", "test_section", new List<string>{"test-class"}, true)
{
    <p>html content goes here</p>
}

and which would generate the following html code : 并且将生成以下html代码:

<section id="test_section" class="collapsible open test-class">
    <div class="collapsible-header">
        <h4>Test block</h4>
    </div>
    <div class="collapsible-content">
        <p>html content goes here</p>
    </div>
<section>

And my problem is that I'm getting the following output : 我的问题是我得到以下输出:

<section id="test_section" class="test-class collapsible open">
    <div class="collapsible-header">
        <h4>Test block</h4>
    </div>
    <div class="collapsible-content">
      EasyFed.Web.Utils.Helpers.CollapsiblePanel
                  {
      <p>html content goes here</p>
                  }
    </div>
    <div class="clearfix"></div>
</section>

As you can see, the name of the method called by the helper is displayed in the html code, and a div with the "clearfix" class appears out of the blue. 如您所见,帮助程序调用的方法的名称显示在html代码中,并且带有“ clearfix”类的div突然显示为蓝色。 Here's the code that generates all of this : 这是生成所有这些代码的代码:

using EasyFed.Web.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Mvc;

namespace EasyFed.Web.Utils.Helpers
{
    public class CollapsiblePanel : IDisposable
    {
        private const string CollapsiblePanelClass = "collapsible";
        private const string CollapsibleHeaderClass = "collapsible-header";
        private const string CollapsibleContentClass = "collapsible-content";
        private const string CollapsibleOpenClass = "open";

        private bool _disposed;
        private readonly FormContext _originalFormContext;
        private readonly ViewContext _viewContext;
        private readonly TextWriter _writer;

        internal CollapsiblePanel(ViewContext viewContext, string panelTitle, string panelId, List<string> classes, bool openByDefault)
        {
            if (viewContext == null)
            {
                throw new ArgumentNullException("viewContext");
            }

            _viewContext = viewContext;
            _writer = viewContext.Writer;
            _originalFormContext = viewContext.FormContext;
            viewContext.FormContext = new FormContext();
            classes = classes ?? new List<string>();

            Begin(panelTitle, panelId, classes, openByDefault);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Begin(string panelTitle, string panelId, List<string> classes, bool openByDefault)
        {
            if (!classes.Contains(CollapsiblePanelClass)) classes.Add(CollapsiblePanelClass);
            if (openByDefault && !classes.Contains(CollapsibleOpenClass)) classes.Add(CollapsibleOpenClass);
            var classesString = "class=\"" + classes.ToSeparatedString(" ") + "\"";

            var sb = new StringBuilder();
            sb.AppendLine(string.Format("<section id=\"{0}\" {1}>", panelId, classesString));
            sb.AppendLine(string.Format("    <div class =\"{0}\">", CollapsibleHeaderClass));
            sb.AppendLine(string.Format("        <h4>{0}</h4>", panelTitle));
            sb.AppendLine("    </div>");
            sb.AppendLine(string.Format("    <div class=\"{0}\">", CollapsibleContentClass));

            _writer.Write(sb.ToString());
        }

        private void End()
        {
            var sb = new StringBuilder();
            sb.AppendLine("    </div>");
            sb.AppendLine("</section>");
            _writer.Write(sb.ToString());
        }

        protected virtual void Dispose(bool disposing)
        {
            if (_disposed) return;
            _disposed = true;
            End();

            if (_viewContext == null) return;
            _viewContext.OutputClientValidation();
            _viewContext.FormContext = _originalFormContext;
        }

        public void EndForm()
        {
            Dispose(true);
        }
    }
}

and here's the helper extension I created : 这是我创建的辅助扩展:

using System.Collections.Generic;
using System.Web.Mvc;

namespace EasyFed.Web.Utils.Helpers
{
    public static class HelperExtensions
    {
        public static CollapsiblePanel BeginCollapsiblePanel(this HtmlHelper htmlHelper, string panelTitle, string panelId, List<string> classes = null, bool openByDefault = false)
        {
            return new CollapsiblePanel(htmlHelper.ViewContext, panelTitle, panelId, classes, openByDefault);
        }
    }
}

If that can be relevant, I wrote this with the help of the following tutorial : http://www.growingwiththeweb.com/2012/09/custom-helper-for-surrounding-block-in.html 如果可以的话,我是在以下教程的帮助下编写的: http : //www.growingwiththeweb.com/2012/09/custom-helper-for-surrounding-block-in.html

Can anyone help me with this ? 谁能帮我这个 ?

Your view code is missing a closing brace, change it to this: 您的视图代码缺少右括号,请将其更改为:

@Html.BeginCollapsiblePanel("Test block", "test_section", new List<string>{"test-class"}, true)
{
    <p>html content goes here</p>
}

At the end, the solution was quite obvious : I had to enclose my helper in a using block so that the Dispose method would be called. 最后,解决方案非常明显:我不得不将助手放在一个using块中,以便调用Dispose方法。 My view code now looks like this : 我的视图代码现在看起来像这样:

@using (Html.BeginCollapsiblePanel("Test block", "test_section", new List<string>{"test-class"}, true))
{
    <p>html content goes here</p>
}

And everything now works just fine ! 现在一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM