Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NETMVC3-将razor标记作为参数传递_C#_Asp.net_Asp.net Mvc 3_.net 4.0_Razor - Fatal编程技术网

C# ASP.NETMVC3-将razor标记作为参数传递

C# ASP.NETMVC3-将razor标记作为参数传递,c#,asp.net,asp.net-mvc-3,.net-4.0,razor,C#,Asp.net,Asp.net Mvc 3,.net 4.0,Razor,我有一个名为EditableArea的助手,它为用户提供运行时可编辑的div(通过JS)。EditableArea helper检查DB中是否存在具有指定ID的可编辑区域(与MVC的区域不相关),如果存在,则呈现该区域的HTML,否则显示作为helper参数指定的默认标记: @Html.EditableArea(someId, "<p>Click to edit contents</p>") @Html.EditableArea(someId,单击编辑内容) 一切正常

我有一个名为
EditableArea
的助手,它为用户提供运行时可编辑的
div
(通过JS)。EditableArea helper检查DB中是否存在具有指定ID的可编辑区域(与MVC的
区域不相关),如果存在,则呈现该区域的HTML,否则显示作为helper参数指定的默认标记:

@Html.EditableArea(someId, "<p>Click to edit contents</p>")
@Html.EditableArea(someId,单击编辑内容

一切正常,但我想更改它,使默认标记不是以字符串形式指定的,而是以razor语法指定的,例如:

@using (Html.EditableArea(someId))
{
    <p>Click to edit contents</p>
}
@使用(Html.EditableArea(someId))
{
单击以编辑内容

}
或者类似的东西,比如MVC3中
@部分的工作方式

我怎样才能做到这一点

我可以使
IDisposable
在其
Dispose
中关闭标记生成器,等等,但是使用这种方法,标记仍然会被呈现(我可以清除
Dispose()
中呈现的内容,但代码块仍然会不必要地运行,我希望避免)


是否有其他方法将razor块传递给帮助程序,这些帮助程序块可能会被实际呈现,也可能不会被实际呈现?

下面是一个示例,我通过为模板本身传递模板Id和razor样式语法来呈现jQuery模板标记:

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>");
}
public static MvcHtmlString jQueryTmpl(此HtmlHelper HtmlHelper,
字符串模板ID,函数模板)
{
返回MvcHtmlString.Create(“+template.Invoke(null)+”);
}
这将被称为

@Html.jQueryTmpl("templateId", @<text>any type of valid razor syntax here</text>)
@Html.jQueryTempl(“templateId”,此处@任何类型的有效razor语法)

基本上只需使用
Func
作为参数和
template.Invoke(null)
(必要时使用参数)来呈现它。显然,您可以跳过对
.Invoke()
的调用,以避免呈现“默认”标记。

只是扩展接受的答案,因为我花了很长时间才解决了一个类似的问题,这就是突然出现的问题。我真正需要的是一个
@helper
,它可以接受razor文本,因为模板应该包含相当多的代码。我玩了很长时间,试图使用几种版本的
@helper item(Func-input)
,但都没有成功。因此,我采取了如下方法:

namespace project.MvcHtmlHelpers
{
    public static class HelperExtensions
    {
        public static MvcHtmlString RazorToMvcString(this HtmlHelper htmlHelper, Func<object, HelperResult> template)
        {
            return MvcHtmlString.Create(template.Invoke(null).ToString());
        }
    }
}
namespace project.MvcHtmlHelpers
{
公共静态类HelperExtensions
{
公共静态MvcHtmlString RazorToMvcString(此HtmlHelper HtmlHelper,Func模板)
{
返回MvcHtmlString.Create(template.Invoke(null.ToString());
}
}
}

@project.MvcHtmlHelpers
@辅助项(其他输入,MvcHtmlString内容)
{
…其他东西。。。
@内容
}
并通过

@item(other input, @Html.RazorToMvcString(@<text>this is a test</text>))
@item(其他输入,@Html.RazorToMvcString(@这是一个测试))

现在我可以使用助手模板进行两种Razor输入,但也可以插入局部视图,这在某些情况下非常方便。由于我不是专家,可能有更好的选择,但对我来说这似乎是一种灵活的方法。

更进一步,可以直接将标记传递给助手,而无需扩展方法

@helper HelperWithChild(Func<object, HelperResult> renderChild)
{
    <div class="wrapper">
        @renderChild(this)
    </div>
}

@HelperWithChild(@<h1>Hello</h1>)
@helper-HelperWithChild(Func-renderChild)
{
@renderChild(此)
}
@HelperWithChild(@Hello)
对于多行标记,还需要

@HelperWithChild(@<text>
    @AnotherHelper()

    <h1>
        With more markup
    </h1>
</text>)

@helper AnotherHelper()
{
    <p>
        Another helper
    </p>
}
@HelperWithChild(@
@另一个助手()
加上更多的标记
)
@助手另一个助手()
{

另一个助手

}

虽然我不确定
这个
将如何与
模型
配合使用-我的助手只使用他们的参数。

如果您想知道这是如何在asp.net core 3.1中实现的

@{
void TemplateFunc(Func模板)
{
@模板(空)
}
}
然后在标记中,您可以将其用作


@{TemplateFunc(@123);}

它不起作用。无法识别函数调用中的
..
语法(无效的表达式术语“太好了,现在可以使用了)。
完全不需要标记(
@
就足够了)除非razor标记实际上是没有任何标记的纯文本,否则只需
@
就足够了。关于
标记,这是正确的。我倾向于使用它们,以防我更改模板中的内容-如果你愿意,这是未来的证明。为什么它会将null传递给模板函数?参数做什么?'IHtmlHelper'DON'ot包含“jQueryTempl”的定义,并且找不到可访问的扩展方法“jQueryTempl”,该扩展方法接受类型为“IHtmlHelper”的第一个参数(是否缺少using指令或程序集引用?)没有mvc核心??
@HelperWithChild(@<text>
    @AnotherHelper()

    <h1>
        With more markup
    </h1>
</text>)

@helper AnotherHelper()
{
    <p>
        Another helper
    </p>
}