I have a custom implementation of a block that works much like Html.BeginForm()
. The implementation is basically as follows:
public class MyBlock : IDisposable {
private readonly HtmlHelper _html;
public MyBlock(HtmlHelper hml) {
this._html.ViewContext.Writer.WriteLine("BEGIN");
}
public void Dispose() {
this._html.ViewContext.Writer.WriteLine("END");
}
}
Then in my view I can do:
@using (new MyBlock(Html)) {
@: some content
}
To get:
BEGIN
some content
END
This all works fine. However, I run into trouble when using my block inside a "razor snippet", e. g. when passing some razor content to a function which takes a Func<object, HelperResult>
as an argument. For example, I have another HtmlHelper function defined as follows:
public static IHtmlString Content(this HtmlHelper @this, Func<object, HelperResult> razor) {
return razor(null);
}
@* use in a view as: *@
@{
var razorContent = Html.Content(@<div>Some razor content</div>);
}
@razorContent
When I do the following, though, the inner content renders without the outer content:
@{
var content =Html.Content(
@<text>
@using (new MyBlock(Html)) {
@: some content 2
}
<text>
);
}
@content
I think the issue is that "Html" still refers to the HtmlHelper of the outer context, and thus BEGIN and END are sent to a different writer than "some content 2", however, I'm not sure that this is the case.
Does anyone know (1) what is going wrong and (2) how I can fix it?