I'm trying to render Telerik Reporting (Q2 2014 SP1 - trial version) in my ASP.NET MVC 4 application (which uses .NET 4.5 framework), but for some reason "Telerik." namespace cannot be resolved in ASP.NET MVC Razor view, although I can access "Telerik." namespace in controller.
Razor view:
#using Telerik - error is in here
#Html.TelerikReporting() - error is in here
Web.config:
<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" />
<add namespace="Telerik.Reporting" />
<add namespace="Telerik.ReportViewer.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>
Telerik libraries my ASP.NET MVC project is referencing:
Telerik.Reporting
Telerik.ReportViewer.Mvc
What can be the issue with my set up?
I solved my problem.
I've been desperately trying following approaches: running solution in VS2012, deleted bin, obj folders, moving libraries from one path to another, restarting machine and VS numerous times and everything was futile.
However I've noticed that Telerik.Reporting.dll and Telerik.ReportViewer.Mvc.dll were not getting copied in my bin folder. In Visual Studio I expanded "References", clicked on each dll file and went to each dll's "Properties" and toggled "Copy Local" property from "False" to "True".
I cleaned and rebuilt my MVC project, checked that the Telerik dll files appeared in bin folder, went to my Razor view and #Html.TelerikReporting() worked!
Out of curiosity I toggled "Copy Local" property from "True" to "False", repeated the rest of the process and #Html.TelerikReporting() still worked. I don't know if there is a problem with Visual Studio or my "Professional" installation.
You shouldn't need a
#using Telerik
if the appropriate namespaces are imported in your web.config. Sometimes in order for your Razer views to pick up on the web.config namespaces after being initially added, you need to restart visual studio for some reason.
I would validate that "Telerik" is the appropriate namespace for the TelerikReporting method you're trying to access.
Additionally, you have a syntax error in:
#Html.TelerikReporting()
It should (likely) be
#(Html.TelerikReporting())
Related
I have a MVC5 project I am trying to solve some issues with where I am not getting intellisense on the System.Web.MVC components
In the Web.Config in my views folder I have the following;
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Configuration" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Helpers" />
<add namespace="Common.Net.Mvc" />
<add namespace="Common" />
<add namespace="MvcSiteMapProvider.Web.Html" />
<add namespace="MvcSiteMapProvider.Web.Html.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>
However when I then reference for example Url., ViewBag or Html.ActionLink etc(also if I reference my Common class which is a .resx generated file). I get errors saying they do not existing the current context.
I can compile and run the code successfully and the pages render just fine, it would be nice to eliminate these errors though and get the intellisense I usually get.
I am running VS2017 Community and I have the Razor extension installed and updated. I have intellisense on other projects it just seems to be this one.
Ok, After much searching and wasting of time, this answer here fixed the issue;
The name 'ViewBag' does not exist in the current context - Visual Studio 2015
Here is a run down of the steps I took (the removal and deletion of files may not be required);
Delete the .suo and .csproj.user from the solution having the issues.
Delete the hidden .vs folder form the route of the solution
Delete the temp asp net files from
C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files
Delete the files fro here;
C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
Note the above alone didn't fix so then I applied the following in conjunction witht he above.
Compared the .csproj version info with that of the project with the issue and ammend (as it was indeed a project carried over form VS 2012) and as per the link above.
Right click the solution -> 'Manage Nuget Packages...' -> Updates -> select all -> apply updates
This rebuilt the nuget references and the intellisense came back. Hopefully one day, somewhere this will help.
I can't get intellisense for my own html helpers. My CustomHtmlHelpers.cs looks like this:
using System.Web.Mvc;
using System.Text;
using System.Web;
namespace laget.Web.Helpers
{
public static class CustomHtmlHelpers
{
//MY HELPERS
}
}
and in my Web.config:
<pages>
<namespaces>
<add namespace="laget.Web.Helpers" />
<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.WebPages"/>
<add namespace="System.Web.Helpers" />
</namespaces>
</pages>
If I put <#using laget.Web.Helpers> in my view, I get the intellisense issue fixed.
Should it not be enough with the code in Web.config?
Sometimes it doesn't seem to work right away. Try closing the .cshtml file, and re-opening it. Then if that doesn't work, try restarting Visual Studio. Also make sure you actually compiled your project, intellisense won't work with non-compiled helpers.
I'm pretty sure that you're not editing the correct Web.config file.
You need to add your namespace to the one in your Views directory.
<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" />
<add namespace="laget.Web.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
You actually don't need to restart Visual Studio in most cases. All you need to do is close the .cshtml file and reopen it!
It needs it on the local page. I'm pretty sure this has to do with Namespace resolution. It isn't exactly sure what you are referring to without the local using statement.
I ran into this today as well. Sometimes just closing the Razor view's window in Visual Studio and re-opening it will do the trick without having to do a full Visual Studio restart.
I tried to solve an issue like this one yesterday. I had e pre-compiled dll (project name ie: MyHtmlHelpers) containing helpers and lot of other classes.
I had the assembly referenced in the web project and the all "standard"-helpers showed up in intellisense but, even though I added the namespace to both web.config in the root and in the views-folder nothing worked. When running the project helpers works, but not in intellisense.
I added a new class and wrote a new html helper inside the web project, added the namespace to web.config. And that worked.
After some hours add tried my last card, adding the MyHtmlHelpers-project to the same solution as my webproject. That did the trick. I diden't change anything in the configs just added the project to the same solution and changed the reference to point at the project insted of the compiled dll.
Isen't that strange? A VS-bug?
I found that i was adding the reference to the wrong web.config. It's not the main config but the web.config in the views directory...
So now I will show you the steps
1.Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)
2.Add the MVC (5.0) nuget package (
right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
3.Close any and all open .cshtml files
4.Right click project -> Properties -> Build -> change Output path to your project “bin/”
5.Add the following minimal Web.config to the root of your class library project
( the web config file is solely needed for intellisense. Configuration (via Web.config)
should be done in the WebApplication hosting your ClassLibrary assembly)
6.Clean and Build the solution.
7.Open cshtml file and try now :)
I found that if it still doesn't work, you may need to go to the properties of the custom class and change the build action from "content" to "compile". That resolved it for me.
I try all of this solutions, one more thing which i didnt find is that in root web.config i must change webpages:Version from 2.0.0.0 to 3.0.0.0.
Open and close all .cshtml files and it's worked.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
I have no intellisense in my cshtml. I have in my web.config (views folder):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.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"/>
<add namespace="EO2.Models" />
<add namespace="EO2" />
<add namespace="EO2.Resources"/>
</namespaces>
</pages>
</system.web.webPages.razor>
only when adding the #using MyNamespace in my cshtml it works.
I checked all versions numbers as desribed here:
MVC Razor view Intellisense broken in VS 2013/2015/2017
Intellisense in razor files (.cshtml) not work
Edit: Problem is in VS or other project files?
I have the same problem also in every new solution.
Tools->Import/Export Settings->Reset Settings
did it for me
tldr; Try switching build configurations and also deleting obj
I haven't had this problem for YEARS and suddenly started getting it.
>------ Build started: Project: MyMVC, Configuration: Debug Any CPU ------
6>S:\TFS\RRStore\RRStore.Main\MyMVC\obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Category\SearchPanel\Search.cshtml.cs(22,26,22,27): error CS0116: A namespace cannot directly contain members such as fields or methods
========== Build: 5 succeeded, 1 failed, 9 up-to-date, 0 skipped ==========
Wait a minute! I'm doing a DEBUG build and it is complaining about obj\Release
So I deleted obj completely and got this error:
15>------ Rebuild All started: Project: MicroPedi_MVC, Configuration: Debug Any CPU ------
15>...RazorGenerator.MsBuild.targets(44,9): error : Could not find a part of the path 'S:\TFS\RRStore\RRStore.Main\My_MVC\obj\Release\Package\PackageTmp\Areas\Admin\Views\CallCenter\Index.cshtml'.
========== Rebuild All: 14 succeeded, 1 failed, 0 skipped ==========
What!! That folder doesn't even exist (obj\Release)!
So I switched to Release configuration. Everything built. Now if I recall this doesn't prove anything because I think I'm using pre-compiled views - tbh I forget how it works.
Anyway THEN I switched back to Debug configuration.
Everything suddenly worked!
If that doesn't help - just try some basic detective work and assume nothing is working!
I found it was building fine on my server and I had made no changes to my web.config in a long long time.
Try making an inconsequential change to your file (to make sure the codegen is updating). eg. add a comment, classname or change some text
Find the generated file which will be something like \obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Products.cshtml.cs
See if your trivial change is reflected there
If not delete everything in \obj\CodeGen\... or probably in obj
Build your project
Another possibility:
I had recently installed .NET Framework 1.1 (to be able to extract a 3rd party library I needed).
I uninstalled it anyway, but not clear if this was the reason anything got screwed up. Suspicious though - especially since 1.1. always liked to mess with IIS.
I would suggest to:
Create a new MVC5 vs2019 project.
Navigate to web.config that resides under the views folder
Replace the 'broken' project web.config with the one from point 2
Fix missing project references
This should fix the error and make build possible again.
I am trying to configure RazorEngine so that intellisense works on views. I add RazorEngine and Microsoft.AspNet.Mvc using nuget. I create TestView.cshtml and declare #model MyModel but it says The name 'model' does not exist in the current context. I also cannot use intellisense inside the view.
Do I miss any step here? How to enable intellisense in the View?
You can use
#using RazorEngine.Templating
#using Namespace.Of.My.Model
#inherits TemplateBase<MyModel>
on the top of your template.
This works fine on a new Console Application with Visual Studio 2013 (after adding a reference to RazorEngine).
The documentation for this is here.
EDIT:
I noticed that this only works when the RazorEngine project is added to the solution and directly referenced. If you use the NuGet package you additionally need to ensure one of the following to make it work:
Your project output path is set to bin\ instead of bin\Debug\ and bin\Release\.
Copy RazorEngine.dll and System.Web.Razor.dll to bin\
I know this question is kind of old. I could not get anything to work, no matter the solution.
I have a hack fix that may be palatable to some. I don't like it very much, but it's the most usable thing I've gotten so far.
The trick is to define the "Model" yourself as a variable from the actual Model. I defined it as "TrueModel", but whatever name you can think of that doesn't collide with "model" or "Model" should work. Then just replace all of your instances of "Model" with "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>
It's not a great solution, but it might be useful.
Oh, I faced with such problem while adding Razor Engine to my custom dll project. To solve this you have to:
1.correctly setup namespaces in web config file(hope you have it in views folder, if not - copy from MVC project):
<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.use to build into bin\ path(not any other, you may use copy post-build command to move results to another place)
3.clean solution and remove obj and bin folders, than build
My views' code starts from #model MyModelClass and all works fine
I'm having an issue where I keep getting error message CS0246 on my web server, but not in my visual studio environment. The issue is coming from a dll I reference in my project. The server cannot find my namespace for my dll.
To solve this I tried copying every file in my project directory to my web server, adding a tag in my web.config file, and adding an import statement in my aspx page. I have also tried adding an assembly reference in my aspx page, but that seems to require a change in the registry which I don't think I have access to.
I should also note my dll and namespace name are different, but changing the names of either did nothing. I also completely removed the dll and copied the code files directly into my project, but it still could not find the namespace.
I thus think I'm supposed to register the namespace and dll somewhere else, but I cannot figure out where. Any help will be appreciated.
If it matters, both my web project and dll are using .NET2.0
I know this is the equivalent of the annoying "did you reboot", but have you tried making a new solution and re-adding the projects and references? Sometimes the .sln file can get corrupted.
Sometimes, the web.config file contains instructions for the .NET framework to generate temporary files.
Please check your config file for something similar to:
<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="YourNamespace.ShouldBeCorrect.Here" />
</namespaces>
</pages>
</system.web.webPages.razor>
This is likely the case if your error page contains a reference to a dynamically generated file such as:
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\133f2363\4ba6d0ba\App_Web_index.cshtml.a8d08dba.ociepbng.0.cs Line: 27