1

我目前正在评估 RazorEngine 以查看它是否可以在我们的项目中使用 - 我们需要动态重新排序视图中的字段,因此我正在尝试向 RazorEngine 发送一个字符串以获取完整的 HTML 并将其显示给用户。

我已经将 RazorEngine 与 @Html.LabelFor() 一起使用,它工作正常。我在这篇 SO 帖子中使用了 mao47 的代码: https ://stackoverflow.com/a/19434112/2878135 ,它基于 Abu Haider 的代码: http ://www.haiders.net/post/HtmlTemplateBase.aspx

但这不适用于 @Html.EditorFor() 。我抛出 System.NotImplementedException - 方法或操作未实现。这是来自一个新的 MVC4 项目。除了实现来自 mao47 的代码外,没有进行任何代码更改。

我有 RazorEngine 源代码,在调用 Execute() 时出现错误

ITemplate.Run(ExecuteContext context)

堆栈跟踪如下所示:

[NotImplementedException: The method or operation is not implemented.]
System.Web.HttpContextBase.get_Items() +29
System.Web.Mvc.Html.TemplateHelpers.GetActionCache(HtmlHelper html) +93
System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +132
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1646
System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +94
System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +228
System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +140
System.Web.Mvc.Html.EditorExtensions.EditorFor(HtmlHelper`1 html, Expression`1 expression) +93
CompiledRazorTemplates.Dynamic.aadebbaabbddd.Execute() +329
RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateBase.cs:126
RazorEngine.Templating.TemplateService.Run(ITemplate template, DynamicViewBag viewBag) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:608
RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:439
RazorEngine.Razor.Parse(String razorTemplate, T model) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Razor.cs:263
TestRazorEngine.Controllers.AccountController.Test() in c:\~\TestRazorEngine\Controllers\AccountController.cs:47
4

2 回答 2

1

原因是我的 Html 模板中没有 HttpContext 或 WebPageContext。我已经在 Controller Action 中 Calling RazorEngine.Parse() 中回答了这个问题,并带有错误的 HttpContextBase代码,但本质上您必须像往常一样调用控制器操作(返回 PartialView(model))。在 .cshtml 调用 RenderAction() 中指定另一个控制器操作,该操作获取模型并使用模板调用 RazorEngine(在我的情况下只是一个字符串)。

这样,当 RazorEngine 从 Execute() 调用它时,MVC 可以完全使用上下文。

于 2013-12-13T11:24:54.160 回答
0

EditorFor 需要在您的子视图文件夹中有一个名为 EditorTemplates 的文件夹,因此您有一个像这样的目录树 /Views/yourViews/EditorTemplates 我给你一个例子

在主视图中

@Html.EditorFor(m => m.Destinatario)

在子文件夹中的文件 Destinatario.cshtml

@model Domain.Models.Entities.Destinatario

<div class="item-destinatario">
<span data-action="view-destinatario">@Model.Nome</span>    

@Html.HiddenFor(m=>m.Id)
@Html.HiddenFor(m=>m.Tabella)
@Html.HiddenFor(m => m.Nome)
</div>

编辑:我忘了型号

public class Destinatario
{
    public int Id { get; set; }
    public string Tabella { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string Cellulare { get; set; }
    public string Fax { get; set; }
}
于 2013-12-11T15:49:24.983 回答