Asp.net mvc 3 创建MVC3 Razor帮助程序,如Helper.BeginForm()

Asp.net mvc 3 创建MVC3 Razor帮助程序,如Helper.BeginForm(),asp.net-mvc-3,razor,html-helper,Asp.net Mvc 3,Razor,Html Helper,我想创建一个可以像helper.BeginForm()那样在括号之间添加内容的助手。我不介意为我的助手创建一个开始,结束,但这样做非常简单和容易 基本上,我试图做的是在这些标记之间包装内容,以便它们已经格式化 差不多 @using Html.Section("full", "The Title") { This is the content for this section <p>More content</p> @Html.TextFor("text","label")

我想创建一个可以像helper.BeginForm()那样在括号之间添加内容的助手。我不介意为我的助手创建一个开始,结束,但这样做非常简单和容易

基本上,我试图做的是在这些标记之间包装内容,以便它们已经格式化

差不多

@using Html.Section("full", "The Title")
{
This is the content for this section
<p>More content</p>
@Html.TextFor("text","label")
etc etc etc
}
@使用Html.Section(“完整”、“标题”)
{
这是本节的内容
更多内容

@Html.TextFor(“文本”、“标签”) 等等等等等等 }
参数“full”是该div的css id,“title”是该节的标题

除了做我想做的事情,还有更好的方法来实现这一点吗


提前感谢您的帮助。

这是完全可能的。在MVC中使用类似于
Helper.BeginForm
的方法是,函数必须返回一个实现
IDisposable
的对象

定义了一个名为
Dispose
的方法,该方法在对象被垃圾回收之前调用

在C#中,
using
关键字有助于限制对象的作用域,并在对象离开作用域时对其进行垃圾收集。因此,将它与
IDisposable
一起使用是很自然的

您需要实现一个
部分
类,该类实现了
IDisposable
。它必须在构建节时呈现该节的打开标记,在释放该节时呈现关闭标记。例如:

public class MySection : IDisposable {
    protected HtmlHelper _helper;

    public MySection(HtmlHelper helper, string className, string title) {
        _helper = helper;
        _helper.ViewContext.Writer.Write(
            "<div class=\"" + className + "\" title=\"" + title + "\">"
        );
    }

    public void Dispose() {
        _helper.ViewContext.Writer.Write("</div>");
    }
}

这是我写的一个小例子。它更通用一些,因此您可以将其用于任何标记和任何属性。它被设置为HtmlHelper上的一个扩展方法,因此您可以从Razor中以及代码中使用它

public static class WrapUtil
{
    public static IDisposable BeginWrap(this HtmlHelper helper, string tag, object htmlAttributes)
    {
        var builder = new TagBuilder(tag);
        var attrs = GetAttributes(htmlAttributes);
        if (attrs != null)
        {
            builder.MergeAttributes<string, object>(attrs);
        }
        helper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));

        return new WrapSection(helper, builder);
    }

    private static IDictionary<string, object> GetAttributes(object htmlAttributes)
    {
        if (htmlAttributes == null)
        {
            return null;
        }
        var dict = htmlAttributes as IDictionary<string, object>;
        if (dict != null)
        {
            return dict;
        }
        return HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    }

    private class WrapSection : IDisposable
    {
        private readonly HtmlHelper _Helper;
        private readonly TagBuilder _Tag;

        public WrapSection(HtmlHelper helper, TagBuilder tag)
        {
            _Helper = helper;
            _Tag = tag;
        }

        public void Dispose()
        {
            _Helper.ViewContext.Writer.Write(_Tag.ToString(TagRenderMode.EndTag));
        }
    }
}
公共静态类WrapUtil
{
公共静态IDisposable BeginWrap(此HtmlHelper帮助程序、字符串标记、对象htmlAttributes)
{
var builder=新标记生成器(标记);
var attrs=GetAttributes(htmlAttributes);
如果(属性!=null)
{
生成器。合并属性(属性);
}
helper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
返回新的包装部分(助手、生成器);
}
私有静态IDictionary GetAttributes(对象htmlAttributes)
{
如果(htmlAttributes==null)
{
返回null;
}
var dict=作为索引的HtmlatAttributes;
if(dict!=null)
{
返回命令;
}
返回HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
}
私有类包装节:IDisposable
{
私有只读HtmlHelper\u助手;
私有只读标记生成器_标记;
公共包装节(HtmlHelper助手、标记生成器标记)
{
_助手=助手;
_标签=标签;
}
公共空间处置()
{
_Write(_Tag.ToString(TagRenderMode.EndTag));
}
}
}

您应该将您的姓名更改为WhatgoodisDispose!!!:)非常感谢你。我知道您必须实现iDisposable,才能使用{}来使用类,但一秒钟都没有想到这一点。再次非常感谢
public static class WrapUtil
{
    public static IDisposable BeginWrap(this HtmlHelper helper, string tag, object htmlAttributes)
    {
        var builder = new TagBuilder(tag);
        var attrs = GetAttributes(htmlAttributes);
        if (attrs != null)
        {
            builder.MergeAttributes<string, object>(attrs);
        }
        helper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));

        return new WrapSection(helper, builder);
    }

    private static IDictionary<string, object> GetAttributes(object htmlAttributes)
    {
        if (htmlAttributes == null)
        {
            return null;
        }
        var dict = htmlAttributes as IDictionary<string, object>;
        if (dict != null)
        {
            return dict;
        }
        return HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    }

    private class WrapSection : IDisposable
    {
        private readonly HtmlHelper _Helper;
        private readonly TagBuilder _Tag;

        public WrapSection(HtmlHelper helper, TagBuilder tag)
        {
            _Helper = helper;
            _Tag = tag;
        }

        public void Dispose()
        {
            _Helper.ViewContext.Writer.Write(_Tag.ToString(TagRenderMode.EndTag));
        }
    }
}