ASP.Net MVC3 - 通剃须刀的标记作为参数
我有一个叫帮手 EditableArea
它为用户提供了一个运行时可编辑 DIV
(通过JS)。如果一个可编辑区域(不涉及MVC的区
)使用指定的ID在数据库中存在EditableArea辅助检查,如果那样的话就使得该地区的HTML,否则会显示默认的标记作为一个参数指定的助手:
I have a helper called EditableArea
which provides a user with a runtime-editable div
(via JS). EditableArea helper checks if an editable area (not related to MVC's Area
) with the specified ID exists in the DB, if so then it renders the area's HTML, otherwise it displays default markup specified as a parameter to the helper:
@Html.EditableArea(someId, "<p>Click to edit contents</p>")
这一切工作正常,但我想改变它,使默认的标记被指定而不是作为一个字符串,但在剃刀语法,是这样的:
It all works ok, but I'd like to change it so that the default markup is specified not as a string but in razor syntax, something like:
@using (Html.EditableArea(someId))
{
<p>Click to edit contents</p>
}
或者类似的东西,喜欢的方式 @section
S在MVC3工作。
Or something similar, like the way @section
s work in MVC3.
我怎样才能做到这一点?
How can I achieve that?
我可以做一个的IDisposable
这在它的处置
关闭TagBuilder等,但我不能避免渲染即使Helper发现有一个ID的区域内剃刀标记(我可以清除处置所呈现的内容(),但里面的{}将继续运行,这是我想跳过code)。
I can make an IDisposable
which in it's Dispose
closes the TagBuilder, etc., but I cannot avoid rendering the inner Razor markup even if the helper finds an area with an ID (I can clear the rendered contents in Dispose() but the code inside the { } would still run, which I'd like to skip).
如果我不使用使用
有通剃刀块的帮手,这可能会或可能不会实际上是由助手提供的一些其他的方式?
And if i don't use using
is there some other way to pass a razor block to the helper, which may or may not be actually rendered by the helper?
下面是我用它来呈现jQuery的模板标记通过传递一个模板ID和刀片式语法模板本身的一个例子:
Here's an example I use to render jQuery Template markup by passing in a template Id and razor-style syntax for the template itself:
public static MvcHtmlString jQueryTmpl(this HtmlHelper htmlHelper,
string templateId, Func<object, HelperResult> template)
{
return MvcHtmlString.Create("<script id=\"" + templateId +
"\" type=\"x-jquery-tmpl\">" + template.Invoke(null) + "</script>");
}
和这将是调用
@Html.jQueryTmpl("templateId", @<text>any type of valid razor syntax here</text>)
基本上只是使用 Func键&LT;对象,HelperResult&GT;
作为参数, template.Invoke(空)
(带如果有必要参数)来渲染它。很明显,你可以跳过调用 .Invoke()
为避免默认的标记。
Basically just use Func<object, HelperResult>
as your parameter and template.Invoke(null)
(with arguments if necessary) to render it. Obviously you can skip the call to .Invoke()
to avoid rendering the "default" markup.