我正在评估RazorEngine,看看它是否可以在我们的项目中使用 - 我们需要动态地重新排序视图中的字段,所以我正在尝试将字符串发送到RazorEngine以获取完整的HTML并显示对用户而言。
我在@ Html.LabelFor()中使用了RazorEngine,它运行正常。我在这篇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
答案 0 :(得分:1)
原因是我的Html模板中没有HttpContext或WebPageContext。 我在Calling RazorEngine.Parse() in Controller Action fails with bad HttpContextBase中用代码回答了这个问题,但实质上你必须像往常一样调用控制器动作(返回PartialView(模型))。在.cshtml中调用RenderAction(),指定另一个获取模型的控制器动作,并使用模板调用RazorEngine(在我的例子中只是一个字符串)。
这样,当RazorEngine从Execute()调用它时,MVC可以使用上下文。
答案 1 :(得分: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; }
}