ASP.NET MVC 中帮助程序的内联 HTML 语法
我有一个类,它扩展了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需推迟操作的执行即可。这是一个例子:
可以这样使用:
You only need to defer the execution of the action. Here's an example:
Which could be used like this: