为自己的助手创建使用?像 Html.BeginForm

发布于 2024-12-12 13:30:38 字数 386 浏览 1 评论 0原文

我想知道,是否可以使用 using 创建您自己的帮助器定义?例如以下创建表单的内容:

using (Html.BeginForm(params)) 
{
}

我想制作我自己的助手。所以我想做一个简单的例子

using(Tablehelper.Begintable(id)
{
    <th>content etc<th>
}

,它将在我的视图中输出

<table>
  <th>content etc<th>
</table>

这可能吗?如果是这样,怎么办?

谢谢

I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form:

using (Html.BeginForm(params)) 
{
}

I'd like to make my own helper like that. So a simple example I'd like to do

using(Tablehelper.Begintable(id)
{
    <th>content etc<th>
}

which will output in my view

<table>
  <th>content etc<th>
</table>

Is this possible? if so, how?

Thanks

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

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

发布评论

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

评论(2

与他有关 2024-12-19 13:30:38

当然,这是可能的:

public static class HtmlExtensions
{
    private class Table : IDisposable
    {
        private readonly TextWriter _writer;
        public Table(TextWriter writer)
        {
            _writer = writer;
        }

        public void Dispose()
        {
            _writer.Write("</table>");
        }
    }

    public static IDisposable BeginTable(this HtmlHelper html, string id)
    {
        var writer = html.ViewContext.Writer;
        writer.Write(string.Format("<table id=\"{0}\">", id));
        return new Table(writer);
    }
}

然后:

@using(Html.BeginTable("abc"))
{
    @:<th>content etc<th>
}

将产生:

<table id="abc">
    <th>content etc<th>
</table>

我还建议您阅读有关 模板化 Razor 委托

Sure, it's possible:

public static class HtmlExtensions
{
    private class Table : IDisposable
    {
        private readonly TextWriter _writer;
        public Table(TextWriter writer)
        {
            _writer = writer;
        }

        public void Dispose()
        {
            _writer.Write("</table>");
        }
    }

    public static IDisposable BeginTable(this HtmlHelper html, string id)
    {
        var writer = html.ViewContext.Writer;
        writer.Write(string.Format("<table id=\"{0}\">", id));
        return new Table(writer);
    }
}

and then:

@using(Html.BeginTable("abc"))
{
    @:<th>content etc<th>
}

will yield:

<table id="abc">
    <th>content etc<th>
</table>

I'd also recommend you reading about Templated Razor Delegates.

你另情深 2024-12-19 13:30:38

是的;但是,要使用 Tablehelper.*,您需要对基本视图进行子类化并添加 Tablehelper 属性。不过,可能更容易的是向 HtmlHelper 添加一个扩展方法:

public static SomeType BeginTable(this HtmlHelper html, string id) {
    ...
}

这将允许您编写:

using (Html.BeginTable(id))
{
    ...
}

但这反过来又需要各种其他管道(在 BeginTable< 处启动元素) /code>,并在返回值上的 Dispose() 中结束)。

Yes it is; however, to use Tablehelper.* you would need to subclass the base-view and add a Tablehelper property. Probably easier, though, is to add an extension method to HtmlHelper:

public static SomeType BeginTable(this HtmlHelper html, string id) {
    ...
}

which will allow you to write:

using (Html.BeginTable(id))
{
    ...
}

but this will in turn require various other bits of plumbing (to start the element at BeginTable, and end it in Dispose() on the returned value).

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