我正在尝试配置RazorEngine,以便智能感知在视图上工作。我使用nuget添加RazorEngine和Microsoft.AspNet.Mvc。我创建了TestView.cshtml并声明了@model MyModel
,但它显示为The name 'model' does not exist in the current context
。我也不能在视图中使用智能感知。
我在这里错过了什么步骤吗?如何在视图中启用智能感知?
发布于 2014-12-27 20:28:16
您可以使用
@using RazorEngine.Templating
@using Namespace.Of.My.Model
@inherits TemplateBase<MyModel>
在您的模板顶部。
这在Visual Studio2013的新控制台应用程序上工作得很好(在添加了对RazorEngine的引用之后)。有关这方面的文档是here。
编辑:
我注意到,只有在将RazorEngine项目添加到解决方案中并直接引用时,这才能起作用。如果您使用NuGet包,则还需要确保以下条件之一才能使其正常工作:
将项目输出路径设置为
bin\
而不是bin\Debug\
,并将RazorEngine.dll
和System.Web.Razor.dll
设置为bin\
发布于 2016-08-03 00:47:31
我知道这个问题有点老了。无论解决方案是什么,我都不能让任何东西工作。我有一个可能会让一些人喜欢的hack修复方法。我不是很喜欢它,但它是迄今为止我得到的最有用的东西。
诀窍是将“模型”自己定义为实际模型中的变量。我把它定义为"TrueModel",但无论你能想到什么名字,它都不会与“模型”或“模型”相冲突。然后只需将"Model“的所有实例替换为"TrueModel”。
@using Namespace.To.My.Models
@* This line should still look like an error,
but we only really care about the intellisense in the rest of the .cshtml file. *@
@{ ModelType TrueModel = (ModelType)Model; }
<div>
@TrueModel.MyProperty is here now.
</div>
<p> @TrueModel.MyOtherProperty is great! </p>
这不是一个很好的解决方案,但它可能会有用。
发布于 2014-12-27 20:41:34
哦,我在将Razor引擎添加到我的自定义dll项目中时遇到了这样的问题。要解决此问题,您必须:
1.在web配置文件中正确设置命名空间(希望您将其放在views文件夹中,如果没有,请从MVC项目中复制):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
</namespaces>
</pages>
</system.web.webPages.razor>
...
2.用于构建bin\
路径(不是任何其他路径,您可以使用copy post-build命令将结果移动到另一个位置)
3.清理解决方案并删除obj
和bin
文件夹,然后构建
我的视图代码从@model MyModelClass
开始,一切正常
https://stackoverflow.com/questions/26862336
复制相似问题