Razor Helpers 与代码块共享 html 问题

发布于 2024-11-27 12:29:19 字数 1025 浏览 0 评论 0原文

我想我想要做的是将我的数据“链接”下来,以便它最终看起来相同。 我所有的 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 技术交流群。

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

发布评论

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

评论(2

晨敛清荷 2024-12-04 12:29:19

查看 Phil Haack 的这篇文章

您可以:

作为强类型 HtmlHelper 的扩展方法编写:

public static class RazorExtensions
{
    public static HelperResult Label<T>(this HtmlHelper<T> helper, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(helper.ViewData.Model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

可以编写

@Html.Label(@<text><span>@Model.Item1<span><strong>@Model.Item2</strong></text>)

将模型作为参数传递给您的辅助方法

public static class FieldSet
{
    public static HelperResult Label<T>(this T model, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

用法:

@FieldSet.Label(Model, @<div><span>@Model.UserName</span><strong>@Model.FullName</strong><p>@Model.Description</p></div>)

Check this articles from Phil Haack

You could:

Write as an extension method to a strongly-typed HtmlHelper:

public static class RazorExtensions
{
    public static HelperResult Label<T>(this HtmlHelper<T> helper, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(helper.ViewData.Model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

So you could write

@Html.Label(@<text><span>@Model.Item1<span><strong>@Model.Item2</strong></text>)

Pass Model as a parameter to your helper method

public static class FieldSet
{
    public static HelperResult Label<T>(this T model, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

Usage:

@FieldSet.Label(Model, @<div><span>@Model.UserName</span><strong>@Model.FullName</strong><p>@Model.Description</p></div>)
蓝眼泪 2024-12-04 12:29:19

您可以查看 @Html.BeginForm 是如何实现的。

创建一个实现 IDisposable 的类,并直接写入响应流:

您的代码可能如下所示(由 head 输入,未测试):

class FieldSet : IDisposable {
     public FieldSet(string label) {
          // TODO: Encode label on line below
          HttpContext.Current.Response.Write(string.Format("<fieldset><label =\"{0}\"", label));
     }

    public void Dispose() {
        HttpContext.Current.Response.Write("</fieldset>");
    }
}

static class FieldSetExtionsions {
    public static FieldSet FieldSet(this HtmlHelper html, string label) {
        return new FieldSet(label);
    }
}

用法将是:

@using (Html.FieldSet("your label")) {
     <div>
         Your razor code goes here
     </div>
}

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):

class FieldSet : IDisposable {
     public FieldSet(string label) {
          // TODO: Encode label on line below
          HttpContext.Current.Response.Write(string.Format("<fieldset><label =\"{0}\"", label));
     }

    public void Dispose() {
        HttpContext.Current.Response.Write("</fieldset>");
    }
}

static class FieldSetExtionsions {
    public static FieldSet FieldSet(this HtmlHelper html, string label) {
        return new FieldSet(label);
    }
}

The usage will be:

@using (Html.FieldSet("your label")) {
     <div>
         Your razor code goes here
     </div>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文