I have just created an ASP.NET MVC 4 project and used Visual Studio 2012 RC to create a Controller and Razor Views for Index
and Create
Actions.
When I came to run the application, and browsed to the Create
view, the following error was shown:
Compiler Error Message: CS0103: The name 'Scripts' does not exist in the current context
The problem is the following code which was added automatically to the bottom of the View:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Why does Scripts not exist?
I looked at the base Web Page class in Assembly System.Web.Mvc.dll, v4.0.0.0
I can see the following helper properties available:
- Ajax
- Html
- Url
But nothing named Scripts.
Any ideas?
EDIT:
My Web.config file looks like this (untouched from the one that Visual Studio created):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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>
</system.web.webPages.razor>
EDIT #2:
People are blogging about using the @Scripts
helper:
Yet having just installed Visual Studio 2012 RC onto a fresh Windows 8 install I am still unable to use @Scripts
even though Visual Studio adds it to the generated View!
Solutions are presented below.
I am not sure how to close this, because in the end an update seemed to resolve the issue. I double checked I had performed a clean install, using a new project. But the same failing project I had made works fine now after various updates and no manual obvious intervention. Thanks for all of the thoughts but there was definitely an issue at the time ;)
I upgraded from Beta to RC and faced 'Scripts' does not exist issue. Surfed all over the web and the final solution is what N40JPJ said plus another workaroudn:
Copy the following in View\Web.config :
and the following inside View\Shared_Layout.cshtml
Hope it helps.
Just write
When i started using MVC4 recently i faced the above issue while creating a project with the empty templates. Steps to fix the issue.
TOOLS
-->Library Package Manager
-->Packager Manager Console
Paste the below command and press enterInstall-Package Microsoft.AspNet.Web.Optimization
Note: wait for successful installation.<namespace="System.Web.Optimization" />
The Package mentioned in step 1 will add few system libraries into the solution references like System.Web.Optimization is not a default reference for empty templates in MVC4.
I hope this helps. Thank you
I had a very similar error when upgrading a project from MVC3 to MVC4.
In my case, I had outdated version numbers in several of my Web.Configs.
Ex:
Be sure to review the Web.Configs in each of your Views directories.
You can read more about Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4.
There was one small step missing from the above, which I found on another post. After adding
to your
~/Views/web.config
namespaces, close and re-open Visual Studio. This is what I had to do to get this working.Import System.Web.Optimization on top of your razor view as follows: