ASP.NET MVC 中帮助程序的内联 HTML 语法

发布于 2024-09-04 11:12:41 字数 1310 浏览 2 评论 0原文

我有一个类,它扩展了 MVC 中的 HtmlHelper 并允许我使用构建器模式来构造特殊输出,例如

<%=
    Html.FieldBuilder<MyModel>(builder => {
        builder.Field(model => model.PropertyOne);
        builder.Field(model => model.PropertyTwo);
        builder.Field(model => model.PropertyThree);
    })
%>

输出一些特定于应用程序的 HTML,让我们说,

<ul>
    <li>PropertyOne: 12</li>
    <li>PropertyTwo: Test</li>
    <li>PropertyThree: true</li>
</ul>

但是,我想做的是添加一个新的构建器方法定义一些内联 HTML,而不必将 is 存储为字符串。例如我想这样做。

<%
    Html.FieldBuilder<MyModel>(builder => {
        builder.Field(model => model.PropertyOne);
        builder.Field(model => model.PropertyTwo);
        builder.ActionField(model => %>
            Generated: <%=DateTime.Now.ToShortDate()%> (<a href="#">Refresh</a>)
        <%);
    }).Render();
%>

并生成这个

<ul>
    <li>PropertyOne: 12</li>
    <li>PropertyTwo: Test</li>
    <li>Generated: 29/12/2008 <a href="#">Refresh</a></li>
</ul>

本质上是一个接受 HTML 块的 ActionExpression。然而,要做到这一点,我似乎需要执行表达式,但将块的执行指向我自己的 StringWriter,我不知道如何执行此操作。有人可以建议吗?

I have a class that extends the HtmlHelper in MVC and allows me to use the builder pattern to construct special output e.g.

<%=
    Html.FieldBuilder<MyModel>(builder => {
        builder.Field(model => model.PropertyOne);
        builder.Field(model => model.PropertyTwo);
        builder.Field(model => model.PropertyThree);
    })
%>

Which outputs some application specific HTML, lets just say,

<ul>
    <li>PropertyOne: 12</li>
    <li>PropertyTwo: Test</li>
    <li>PropertyThree: true</li>
</ul>

What I would like to do, however, is add a new builder methid for defining some inline HTML without having to store is as a string. E.g. I'd like to do this.

<%
    Html.FieldBuilder<MyModel>(builder => {
        builder.Field(model => model.PropertyOne);
        builder.Field(model => model.PropertyTwo);
        builder.ActionField(model => %>
            Generated: <%=DateTime.Now.ToShortDate()%> (<a href="#">Refresh</a>)
        <%);
    }).Render();
%>

and generate this

<ul>
    <li>PropertyOne: 12</li>
    <li>PropertyTwo: Test</li>
    <li>Generated: 29/12/2008 <a href="#">Refresh</a></li>
</ul>

Essentially an ActionExpression that accepts a block of HTML. However to do this it seems I need to execute the expression but point the execution of the block to my own StringWriter and I am not sure how to do this. Can anyone advise?

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

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

发布评论

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

评论(1

风尘浪孓 2024-09-11 11:12:41

您只需推迟操作的执行即可。这是一个例子:

public static class HtmlExtensions
{
    public static SomeClass ActionField<TModel>(this HtmlHelper<TModel> htmlHelper, Action<TModel> action)
    {
        return new SomeClass(() => { action(htmlHelper.ViewData.Model); });
    }
}

public class SomeClass
{
    private readonly Action _renderer;
    public SomeClass(Action renderer)
    {
        _renderer = renderer;
    }

    public void Render()
    {
        _renderer();
    }
}

可以这样使用:

<% Html.ActionField(model => { %>
    Generated: <%=DateTime.Now.ToShortDate()%> (<a href="#">Refresh</a>)
<% }).Render(); %>

You only need to defer the execution of the action. Here's an example:

public static class HtmlExtensions
{
    public static SomeClass ActionField<TModel>(this HtmlHelper<TModel> htmlHelper, Action<TModel> action)
    {
        return new SomeClass(() => { action(htmlHelper.ViewData.Model); });
    }
}

public class SomeClass
{
    private readonly Action _renderer;
    public SomeClass(Action renderer)
    {
        _renderer = renderer;
    }

    public void Render()
    {
        _renderer();
    }
}

Which could be used like this:

<% Html.ActionField(model => { %>
    Generated: <%=DateTime.Now.ToShortDate()%> (<a href="#">Refresh</a>)
<% }).Render(); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文