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 ;)
just remove/ hide the code from create & Edit razor view of your controller.
good luck...
I am using areas, and have just come up against this issue, I just copied the namespaces from the root web.config to the areas web. config and it now works!!
For me this solved the problem, in NuGet package manager console write following:
That has an obvious solution. I had the same problem later. Not related to Assembly References or ... .It'll occur In hierarchy calling of MVC Partial views, when you have complicated page structures. So calling/rendering each part separately on each page (maybe a master page or partial) will cause to not see required parts of page like the bellow code :
That simply forces page to find and render related section and in case of failure shows you an error message like you.
So I suggest you to trace your pages (like program trace) from master to all of its partials to Detect Dependencies. Maybe it be a terrible work, but no other choices available here.
Not that according to my experience, some conditional situations in programming causes not to show you the right error causes the problem.
Create a new MVC 4 RC internet application and run it. Navigate to Login which uses the same code
What allows Login.cshtml to work is the the Views\Web.config file (not the app root version) contains
Why is your Create view not working and Login is?