I notice that some Razor functions take html content as parameters, eg.:
@section Scripts {
<script type="text/javascript">
$(function() {
alert("aha");
});
</script>
}
This doesn't render the html content immediately rather Razor's Section() system stores it for later rendering.
I would like to write my own helper function which accepts html content in the same way, eg.:
@Html.MyCoolFunction {
<div><stuff><blah>...
dafsdfsa
</blah></stuff></div>
}
I don't know what syntax would work, but presumably MyCoolFunction would accept an MvcHtmlString as a parameter and I can do what I want with it.
Is this possible?
I managed to work it out from intellisense & other code samples.
An extension function like this:
public static MvcHtmlString DoTwice(this HtmlHelper htmlHelper, Func<MvcHtmlString, HelperResult> htmlContent)
{
var x = htmlContent(new MvcHtmlString("")).ToHtmlString() +
htmlContent(new MvcHtmlString("")).ToHtmlString();
return new MvcHtmlString(x);
}
Can be called & passed block html content, like this:
@Html.DoTwice(
@<div>What happens??</div>
)
Output:
What happens??
What happens??
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.