创建像 Helper.BeginForm() 这样的 MVC3 Razor Helper

发布于 2024-12-01 11:17:34 字数 428 浏览 0 评论 0原文

我想创建一个助手,我可以像 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
}

“full”是该 div 的 css id,“the title”是该部分的标题。

除了做我想做的事情之外,还有更好的方法来实现这一目标吗?

预先感谢您的任何帮助。

I want to create a helper that i can add content between the brackets just like Helper.BeginForm() does. I wouldnt mind create a Begin, End for my helper but it's pretty simple and easy to do it that way.

basically what i am trying to do is wrapping content between these tags so they are rendered already formatted

something like

@using Html.Section("full", "The Title")
{
This is the content for this section
<p>More content</p>
@Html.TextFor("text","label")
etc etc etc
}

the parameters "full" is the css id for that div and "the title" is the title of the section.

Is there a better way to achieve this other than doing what i am trying to do?

thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2024-12-08 11:17:34

这是完全有可能的。在 MVC 中使用 Helper.BeginForm 之类的方法完成此操作的方式是,该函数必须返回一个实现 IDisposable 的对象。

IDisposable 接口 定义了一个名为 的方法Dispose 在对象被垃圾收集之前调用。

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

您需要实现一个实现 IDisposableSection 类。它必须在构造时呈现您的部分的开放标记,并在处置时呈现关闭标记。例如:

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。

public static MySection BeginSection(this HtmlHelper self, string className, string title) {
    return new MySection(self, className, title);
}

It's totally possible. The way this is done in MVC with things like Helper.BeginForm is that the function must return an object that implements IDisposable.

The IDisposable interface defines a single method called Dispose which is called just before the object is garbage-collected.

In C#, the using keyword is helpful to restrict the scope of an object, and to garbage-collect it as soon as it leaves scope. So, using it with IDisposable is natural.

You'll want to implement a Section class which implements IDisposable. It will have to render the open tag for your section when it is constructed, and render the close tag when it is disposed. For example:

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>");
    }
}

Now that the type is available, you can extend the HtmlHelper.

public static MySection BeginSection(this HtmlHelper self, string className, string title) {
    return new MySection(self, className, title);
}
萌︼了一个春 2024-12-08 11:17:34

这是我为此编写的一个小实用程序。它更通用一些,因此您可以将它用于任何标签和任何属性。它被设置为 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));
        }
    }
}

Here is a little util I wrote to do this. It is a little more generic so you can use it for any tag and with any attributes. It is setup as an extension method on HtmlHelper so you can use it right from within Razor as well as from within code.

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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文