Razor Helpers 与代码块共享 html 问题
我想我想要做的是将我的数据“链接”下来,以便它最终看起来相同。 我所有的 html 都必须以某种形式包装,
<fieldset class="" data-role="">
所以我拥有的是一个打印各种表单的助手。一种是标签:
<fieldset data-role="@role">
<label>@Html.Raw(label)</label>
</fieldset>
现在当我有多种类型的标签时,其中一种包括作为代码块。当它是一个 简单的文本,例如“名字”,我这样做:
@FieldSet.Label("First Name")
但是当我有一个代码块时,例如:
<b>some text</b>
<p>some other text (some time frame - some time frame)
使用它变得复杂:
@FieldSet.Label("<b>" + Model.Text1 + "</b><p>" + Model.Text2 +
" (" + Model.Time1 + " - " + Model.Time2 +")</p>")
我想要一个看起来像这样的解决方案:
@FieldSet.Label(@<text>
<b>@Model1.Text1</b>
<p>@Model.Text2 (@Model.Time1 - @Model.Time2)</p>
</text>)
我在某处读到这是可能的,但我找不到这篇文章。我可能完全被误导了,但我真的不想在后面的代码中有一段 HTML,我想使用 razor 语法,而不是字符串连接。
I guess what I want to do is "chain" my data down so that it ends up looking the same.
All my html must be wrapped in some form of
<fieldset class="" data-role="">
So what I have is a helper that prints the various forms. One would be a label:
<fieldset data-role="@role">
<label>@Html.Raw(label)</label>
</fieldset>
Now when I have multiple types of labels, and one includes being a code block. When it is a
simple piece of text, like "First Name" I do:
@FieldSet.Label("First Name")
But when I have a code block such as:
<b>some text</b>
<p>some other text (some time frame - some time frame)
It becomes complicated to use this:
@FieldSet.Label("<b>" + Model.Text1 + "</b><p>" + Model.Text2 +
" (" + Model.Time1 + " - " + Model.Time2 +")</p>")
What I want it a solution that looks something like this:
@FieldSet.Label(@<text>
<b>@Model1.Text1</b>
<p>@Model.Text2 (@Model.Time1 - @Model.Time2)</p>
</text>)
I read somewhere this was possible, but I cannot find the article. I could be completely misled, but I really don't want to have a single piece of HTML in the code behind and I want to utilize the razor syntax, not string concatenation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Phil Haack 的这篇文章
您可以:
作为强类型 HtmlHelper 的扩展方法编写:
可以编写
将模型作为参数传递给您的辅助方法
用法:
Check this articles from Phil Haack
You could:
Write as an extension method to a strongly-typed HtmlHelper:
So you could write
Pass Model as a parameter to your helper method
Usage:
您可以查看
@Html.BeginForm
是如何实现的。创建一个实现 IDisposable 的类,并直接写入响应流:
您的代码可能如下所示(由 head 输入,未测试):
用法将是:
You could look at how the
@Html.BeginForm
is implemented.Create a class that implements IDisposable, and that writes to the Response stream directly:
Your code could look like this (entered by head, not tested):
The usage will be: