英文:
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
<div>
<div>@Model.Title</div>
<div>BODY(@Model)</div>
</div>
The thing I am looking for is that magic BODY "thing", so I can use the partial like this:
<partial name="_MyPartial" model="someData">
@BODY {
<h1>@Model.Welcome</h1>
<p>@Model.Text</p>
}
</partial>
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(
<h1>Hello</h1>
<p>World</p>
),
};
}
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>
英文:
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 = "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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论