RazorEngine - @Html.LabelFor OK, @Html.EditorFor "not implemented exception" on Execute()



我目前正在评估RazorEngine,看看它是否可以在我们的项目中使用-我们需要动态地重新排列视图中的字段,所以我试图向Razor引擎发送一个字符串,以获得完整的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:_gitRazorEnginesrcCoreRazorEngine.CoreTemplatingTemplateBase.cs:126
RazorEngine.Templating.TemplateService.Run(ITemplate template, DynamicViewBag viewBag) in c:_gitRazorEnginesrcCoreRazorEngine.CoreTemplatingTemplateService.cs:608
RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName) in c:_gitRazorEnginesrcCoreRazorEngine.CoreTemplatingTemplateService.cs:439
RazorEngine.Razor.Parse(String razorTemplate, T model) in c:_gitRazorEnginesrcCoreRazorEngine.CoreRazor.cs:263
TestRazorEngine.Controllers.AccountController.Test() in c:~TestRazorEngineControllersAccountController.cs:47

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

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

EditorFor需要在您的子视图文件夹中有一个名为EditorTemplates的文件夹,所以您有一个像这样的目录树/Views/yourViews/EditorTemplate我给您举了一个示例

在主视图中

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

在子文件夹中,文件Destinatario.chtml

@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; }
}

最新更新