有没有一种方法可以将一个部分的内容作为内部 HTML 提供?

huangapple go评论55阅读模式
英文:

Is there a way to provide the body of a partial as inner HTML?

问题

以下是您要翻译的内容:

@model SomeData

<div>
    <div>@Model.Title</div>
    <div>BODY(@Model)</div>
</div>

我理解您的需求,您正在寻找用于替代 BODY 的特殊功能,以便您可以像这样使用部分:

<partial name="_MyPartial" model="someData">
    @BODY {
        <h1>@Model.Welcome</h1>
        <p>@Model.Text</p>
    }
</partial>

请问您是否已经找到类似的功能或解决方案?

英文:

Let's say this is my partial:

@model SomeData

&lt;div&gt;
    &lt;div&gt;@Model.Title&lt;/div&gt;
    &lt;div&gt;BODY(@Model)&lt;/div&gt;
&lt;/div&gt;

The thing I am looking for is that magic BODY "thing", so I can use the partial like this:

&lt;partial name=&quot;_MyPartial&quot; model=&quot;someData&quot;&gt;
    @BODY {
        &lt;h1&gt;@Model.Welcome&lt;/h1&gt;
        &lt;p&gt;@Model.Text&lt;/p&gt;
    }
&lt;/partial&gt;

Is something similar available already?

*********** UPDATES ****************

I think my question can come down to whether it is possible to do this:

Let's say I define my Model class like this:

public class SomeData 
{
    public HtmlString Body { get; set; }
}

Then in a view:

@{
    var someData = new SomeData
    {
        Body = Html(
            &lt;h1&gt;Hello&lt;/h1&gt;
            &lt;p&gt;World&lt;/p&gt;
        ),
    };
}

Is there some Html helper to achieve this?

答案1

得分: 1

What you need is like a Taghelper. You can try the following code.
SomeData.cs

    public class SomeData
    {
        public string Title { get; set; }
        public string Welcome { get; set; }
        public string Text { get; set; }
    }

MyPartialTagHelper.cs

    public class MyPartialTagHelper : TagHelper
    {
        public SomeData data { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {

            output.TagName = "div";
            output.TagMode = TagMode.StartTagAndEndTag;
            StringBuilder stringb = new StringBuilder();

            stringb.Append("<h1>" + data.Welcome + "</h1>");
            stringb.Append("<p>" + data.Text + "</p>");

            output.Content.SetHtmlContent(stringb.ToString());
        }
    }

Controller

        public IActionResult Index()
        {
            SomeData Data = new SomeData() { Title = "title" };
            return View(Data);
        }

Index.cshtml

@model SomeData
@addTagHelper *,WebApplication4
@{
    var someData= new SomeData() { Welcome="hello",Text="world"};
}

<div>
    <div>@Model.Title</div>
    <my-partial data=someData/>
</div>

Output:
有没有一种方法可以将一个部分的内容作为内部 HTML 提供?

英文:

What you need is like a Taghelper. You can try the follwoing code.<br>
SomeData.cs

    public class SomeData
    {
        public string Title { get; set; }
        public string Welcome { get; set; }
        public string Text { get; set; }
    }

MyPartialTagHelper.cs

    public class MyPartialTagHelper : TagHelper
    {
        public SomeData data { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {

            output.TagName = &quot;div&quot;;
            output.TagMode = TagMode.StartTagAndEndTag;
            StringBuilder stringb = new StringBuilder();

            stringb.Append(&quot;&lt;h1&gt;&quot; + data.Welcome + &quot;&lt;/h1&gt;&quot;);
            stringb.Append(&quot;&lt;p&gt;&quot; + data.Text + &quot;&lt;/p&gt;&quot;);

            output.Content.SetHtmlContent(stringb.ToString());
        }
    }

Controller

        public IActionResult Index()
        {
            SomeData Data = new SomeData() { Title = &quot;title&quot; };
            return View(Data);
        }

Index.cshtml

@model SomeData
@addTagHelper *,WebApplication4
@{
    var someData= new SomeData() { Welcome=&quot;hello&quot;,Text=&quot;world&quot;};
}

&lt;div&gt;
    &lt;div&gt;@Model.Title&lt;/div&gt;
    &lt;my-partial data=someData/&gt;
&lt;/div&gt;

Output<br>
有没有一种方法可以将一个部分的内容作为内部 HTML 提供?

huangapple
  • 本文由 发表于 2023年5月11日 15:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定