Html.EditorFor not loading custom template paths - asp.net-mvc

In my program I have put,
<%: Html.EditorFor(m => m.EducationData
, "~/Views/HTML/Shared/EditorTemplates/Foo/CustomTemplate.ascx")%>
but it doesn't load the editor templates from the path I've given. I've seen in some of the examples,link where they have given custom paths for the templates. Can anyone suggest something? or does MVC2 supports custom paths for editor templates? or Is there a way to customize the web.config or some configurations,so I could change the default template locations???

I think you have too many folders. There is a specific convention you should follow when using EditorTemplates and DisplayTemplates. Try putting your templates in this folder and it should work:
"~/Views/Shared/EditorTemplates/CustomTemplate.ascx"
Update:
Not all of your editortemplates need to go into the Shared folder. You can put controller-specific templates into controller-specific folders too:
"~/Views/Home/EditorTemplates/CustomTemplate.ascx"

Related

Structure in a Blazor App and why can't I have one?

So, I've created a bog-standard Blazor Server App using dotnet new blazorserver. Opened the solution in VS and run it.
Fine.
If I add a new folder, say Components and add a new Razor Component in that folder - Component1.razor - then in my index.razor page I add a using statement to point at my Components folder and the mark up to include the component itself and run the app, the index page shows, but there is no sign of my component. Further looking at the source of the rendered HTML, I see an empty element <component1></component1>
If I move my new component to the Pages folder and rerun the app, the component renders properly.
If I create a sub-folder under Pages and move my component to there and rerun the app, the component fails to render.
Am I doing something wrong? Am I expecting too much? Should I be able to have a structure that means I don't have to have every single component in a single folder?
I think you missing the point of _Imports.razor. You can put your pages anywhere they will be found by the #page "" attribute. If you want your components to be available either put a reference to their folder via the _Imports.razor or use the #namespace attribute/directive to override the namespace from folder its is in to another that is being imported. There is nothing special happening here. The template puts a using statement in for the "Shared" folder. This is why App.razor in the root folder has access to them.
Example _Imports.razor (From a project with name/default namespace of PolymorphicApi)
...
#using PolymorphicApi
#using PolymorphicApi.Shared
If you do not want to use _Imports.razor, you may not want to make all your components available. You can use #namespace in the component. This is the same as overriding the default namespace in a .cs file.
Example :
#namespace PolymorphicApi
A component using this statement could be in any subfolder and will be available as the root namespace is already imported.
As a side note: _Imports.razor can be thought of as a chunk of razor statements that will be imported into all razor components in that folder down. You do not have to use it just for namespaces. For example you can use an #inject statement. I do this to have Localization in every component by default.

How to use two themes in asp.net mvc project

I have two themes' folders called Default and New in Views folder. These folders are included .cshtml extension files. I want to use these folders as theme option. How can I do theme settings in global.asax?
Install bootstrap through visual studio Manage Nugget Packages and you can utilize free themes available. Lot of free themes are available in https://bootswatch.com/. Download a CSS from the mentioned link and replace the one in your bootstrap CSS folder. Hope this helps...
EDIT
Actually in Asp.Net MVC Global.asax has nothing to do with setting themes. Current theme in MVC is decided by the styles specified in _Layout.cshtml file. Usually this file lies inside Views\Shared path. The _ViewStart.cshtml which lies directly under Views folder decides which layout should the view use.
Approach 1
If we require some other layout for certain views we can add another _ViewStart.cshtml inside the folder where those views exist and specify the path of _NewLayout.cshtml in it. The views will automatically get the new layout/theme while you render it.
Approach 2
Use a logic and set your layout/theme accordingly as shown below in /Views/_ViewStart.cshtml file,
#{
if (this.User.IsInRole("Admin") || !this.User.Identity.IsAuthenticated) {
Layout = "~/Views/Shared/_Layout.cshtml";
} else {
Layout = "~/Views/Shared/_LayoutUser.cshtml";
}
}

deployment path issue in css

I have to rewrite a legacy asp.net mvc app using lots of:
Url.Content()
to get it to work in a different/additional deployment environment where the virtual directory is a sub directory of the default site.
At the moment css classes like this:
.icos-pencil:before { content: url(/content/images/global/icons/usual/icon-pencil.png); }
are also broke. Is there a similar 'helper' (?) like Url.Content for the css above?
If you want use helper in js or css file you can write own view engine such as jsHelper or you can use this code
background-image:url('../../content/images/global/icons/usual/icon-pencil.png');
becomes
background-image:url('content/images/global/icons/usual/icon-pencil.png');
Simply use relative path in your CSS.

Relative filename in ERB files

I am putting my ERB files in app/view. The problem is that sometimes I make mistakes when including CSS and JS files. I refer to them as "js/include.js" or "css/default.css" instead of /js/include.js and /css/default.css
However, these files are located in the public directory not the app/views directory so as a result the page breaks.
Is there a way to change the default behavior so that it looks in public folder whenever I refer to these files relatively?
If you stick with the Rails conventions you'll save yourself some grief. Use stylesheet_link_tag and javascript_include_tag in your layouts. And don't scatter css and js includes around your views. Use custom layouts instead.
For example, if you have an admin interface with a different look and different behavior you could add app/views/layouts/admin.html.erb. Then, in AdminController specify that it should use the admin layout. You can also specify the layout at the controller action level if you need to.

Rails Routing INSIDE CSS

Is there any way I can route assets inside of my css to where the rest of the views are pulling them? I mean, inside the CSS can I call url_for or css_for or something like that in order to have the images go through the assets router?
Thank you in advance!
You can use a controller action to render your CSS (with an erb template) and set the content type to text/css.
Take a look at this blog post from Josh Susser on dynamically generated stylesheets. It is from 2006 but the technique described is still applicable.

Resources