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?
What you need is like a Taghelper. You can try the follwoing 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>