Using _ViewImports.cshtml File - asp.net-mvc

I made this _ViewImports.cshtml File, which inside refers to Models namespace in my Models folder.
Now in my Index.cshtml page when I want to remove "#using Razor.Models.Products " I encounter an error
now I'm sure I did it right since when I use the "# using expression" , it says its redundant:
what could be the problem?

Related

IgnoreRoute with file extension is not working in MVC

I have a Content folder and i want all the html files inside the Content folder from getting accessed, so i added this line at the top of the RegisterRoutes() method in the RouteConfig.cs file as follows
routes.IgnoreRoute("{folder}/{resource}.html");
but still i am able to access the file.
I even tried giving a static value like this
routes.IgnoreRoute("Content/{resource}.html");
and like this
routes.IgnoreRoute("Content/demo.html");
but still was able to access the file, am i missing some thing, how can i acheive this behaviour.

ASP.NET MVC locating cs files generated from cshtml?

So I've recently started with ASP.NET MVC 4. I'm using the razor engine.
My question is concerning the view files, with suffix cshtml. It seems to me that these are precompiled into *.cs files by razor which in turn are then compiled into MSIL. (A pattern that is familiar from my days as a JSP developer.) One reason why I am making this assumption is that if I enter some invalid c# code into the cshtml file I get a compilation error displayed like this:
Line 34: public class _Page_Views_BaseDataAdmin_Index_cshtml : ...
And line 34 is not indicative of where the error is in the cshtml file, just as the class _Page_Views_BaseDataAdmin_Index_cshtml seems to refer to a regular .net class not the view file.
So my question is: Where do I find the cs file? Specifically, in the example above, "_Page_Views_BaseDataAdmin_Index_cshtml.cs"? Maybe I need to add some config to tell MVC to keep this .cs file on disk, if so, how do I do this?
Thanks!
A quick tip to find the generated files is to add this to your .cshtml file.
This will show you immediately the full path
<div>This file is compiled to #this.GetType().Assembly.CodeBase</div>
You can find the compiled views in your Temporary ASP.NET Files folder.
First find the .compiled file that corresponds to your view (ex: home.cshtml):
home.cshtml.a8d08dba.compiled
This file contains the assembly name: assembly="App_Web_hkc53urb"
Your .cs file will be the assembly name concatenated with a number: App_Web_hkc53urb.1.cs
An easier approach might be to use Windows search for your view name in the temp ASP.NET directory.

Add a reference to ASP.NET MVC ASPX page

I have some HTML Helpers that I imported from an external project. How do I reference them within the ASPX file?
Currently I get an error similar to the following:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper<LC.TLAdminMVC.DAL.BusinessObjects.CompanySingleViewModel>' does not contain a definition for 'Configurator' and no extension method 'Configurator' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<LC.TLAdminMVC.DAL.BusinessObjects.CompanySingleViewModel>' could be found (are you missing a using directive or an assembly reference?)
To better understand your situation can you answer one question.
Have you placed the helper methods inside the aspx files or are they external?
If they are external:
Make a directory in your solution named: app_code
Create a new file, whatever name (we'll call it 'Content')
When you want to use the helper method just call Content.methodName(params)
If they are internal:
Try checking if they are not nested inside a block.
Hope this helps,

grails render tag: can't find file unless it's located in src/templates/scaffolding?

I tried to follow the example under "Shared Templates" here:
http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.2.3%20Views%20and%20Templates
But this just plain didn't work. The tag I used was:
<g:render template="/includes/mySearch"></g:render>
I created a directory under the views called "includes" and created a gsp file, with a very basic form in it, named mySearch.gsp.
However, grails reported being unable to find the file:
java.io.FileNotFoundException: C:\springsource\sts-2.8.1.RELEASE\grails-1.3.7\src\grails\templates\scaffolding_mySearch.gsp
According to the documentation: "In this case you can place them in the root views directory at grails-app/views or any subdirectory below that location and then with the template attribute use a / before the template name to indicate the relative template path."
It would appear that this is exactly what I did, but grails was not looking there? Any advice?
Many thanks,
Alexx
Template files need to be starting with an underscore. Therefore you need to rename your mySearch.gsp to _mySearch.gsp.

Why can't I include a tilde in a path when using Html.Partial

#Html.Partial("~/Shared/SuccessErrorStatus")
Server Error in '/' Application.
The partial view '~/Shared/SuccessErrorStatus' was not found or no view engine supports the searched locations. The following locations were searched:
~/Shared/SuccessErrorStatus
It seems to give the impression the path has been searched but it doesn't seem to have resolved the path correctly.
You should use either complete name as Darin pointed out or use a View name only.
In this case
#Html.Partial("SuccessErrorStatus")
should work too.
You forgot the extension. Also you probably meant ~/Views/Shared and not ~/Shared. Normally all views should be located inside the ~/Views folder:
#Html.Partial("~/Views/Shared/SuccessErrorStatus.cshtml")

Resources