I'm getting this error in Visual Studio, when I use @Html.Sitecore
:
'System.Web.Webpages.Html.Htmlhelper' does not contain a definition for 'Sitecore' and the best extension methods overload 'Sitecore.Mvc.HtmlHelperExtensions.Sitecore(System.Web.Mvc.HtmlHelper)' has some invalid arguments.`
However, once deployed it does run without any problems.
I'm using Sitecore 7.2 with MVC 5.1.
Articles that I've read that deal with similar error messages, talk about the system.web.webPages.razor
section of the Views
folder web.config
file. This is how it appears in my solution.
<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="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
Originally I thought I'd just need to restart VS, but that didn't work. Does anyone have any suggestions.
EDIT This is the standard Sitecore 7.2 installation, and so has the following binding redirects:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.1.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" xmlns="urn:schemas-microsoft-com:asm.v1"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" xmlns="urn:schemas-microsoft-com:asm.v1"/>
</dependentAssembly>
Also when looking at the output from visual studio there is an additional error that I didn't spot before:
Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper'
EDIT 2
I no longer think this is a Sitecore MVC issue. I get a similar error with @Html.ActionLink("xxx", "xxx")
. Other people have seen this issue in VS2012, but I'm using 2013.
EDIT 3
There are now quite a few answers to this question. I recommend trying each of them as it seems there are numerous reasons this error might occur.
I have now resolved this issue. I simply had to install Update 2 of Visual Studio 2013. How frustrating.
Thanks to StriplingWarrior and Ahmed Okour for your useful advice.
You say you're using MVC 5, but you're referencing version 3 in the code you provided. Here's what mine says:
Try checking through your entire web.config file (or all the web.config files if you have multiple Areas) and making sure that all the versions are set right. For MVC 5,
System.Web.WebPages
should be on version 2 and System.Web.WebPages.Razor should be on version 3....
Try This, then try to restart visual studio.
You forgot to add
<add namespace="Sitecore.Mvc" />
so that intellisense can pick it up in VS.I have added Sitecore.Mvc dll in references with copy local to true and added below tag in view's web.config under namespaces tag, then my problem is resolved.
I have used the below line at the top and the issue has been resolved.