我有一个简单的部分:
<div class="col-xs-12">
<label>view model</label>
<textarea class="form-control btn-xs" rows="12">@JsonConvert.SerializeObject(Model)</textarea>
</div>
以下没有奏效:
@using Newtonsoft.Json
在部分或主要的cshtml中,也不是:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="Newtonsoft.Json" />
</namespaces>
</pages>
</system.web.webPages.razor>
在Views / Web.config中。我还是得到了:
The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
或
The name 'JsonConvert' does not exist in the current context
答案 0 :(得分:1)
我必须将以下内容添加到我的Web.config中以消除此错误
<configuration>
<configSections>
<section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
</configSections>
<razor disableAutoIncludeModelNamespace="false">
<assemblies>
<add assembly="Newtonsoft.Json" />
</assemblies>
<namespaces>
<add namespace="Newtonsoft.Json" />
</namespaces>
</razor>
</configuration>
答案 1 :(得分:0)
愚蠢的问题:您是否已将Json.Net nuget包添加到您的项目中?
最后一次尝试不起作用的最简单的解释就是这样。