Kendo editor templates is not found when placing inside an area - asp.net-mvc

We're using Kendo grid with Editor templates. In the examples provided by Kendo they store the templates in the global Shared folder for all Views: ~/Views/Shared/EditorTemplates.
In our project we use Areas and it is more appropriate and logical to store the templates in a Shared folder of particular Area and Controller. However, the template cannot be found if we store it inside an area.
We tried several variations of the structures:
~/Areas/SomeArea/Views/SomeView/Shared/EditorTemplates/editor.cshtml
~/Areas/SomeArea/Views/SomeView/EditorTemplates/editor.cshtml
And the corresponding path in the grid:
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("/Shared/Editortemplate/editor")
or
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("/Editortemplate/editor")
However, instead of our template the default one appears.
Where do we do this wrong? Thanks!

The name passed to the TemplateName method should be just the name of the template not the whole path, in your case it should be "editor". Rest of the work how this template will be found will be done by the Razor ViewEngine.
I would suggest you to check the following question which is pretty similar. I hope it helps.

Related

Html.RenderPartial - Render Partial View located in another folder

I have my view 'Create.cshtml' in folder Views-Department. I want to use partial view which is located in folder on root, 'CommonViews' with the name '_EnterpriseStructure.cshtml'.
I am using syntax RenderPartial
#{Html.RenderPartial("~/CommonViews/_EnterpriseStructure.cshtml");}
Partial view code
<div id="Client">
This is a Partial View.
</div>
When executed I am getting System.InvalidOperationException.
Detail error is:
The view at '~/CommonViews/_EnterpriseStructure.cshtml' must derive from WebViewPage, or WebViewPage.
I am using ASP.Net MVC 5 Razor Views
I have found that Razor view engine is searching in folders 'Views/Shared', 'Views/Departments' . When I moved the Partial View, to folder 'Shared' it worked.
You cannot include path, you just have to give View name. And add folders to search for View engine.
What I cannot figure out is "How to give more Search locations to View Engine?"
I would reckon you to use T4MVC nuget package in your MVC project. Once the package is included in your project, you just need to select "T4MVC.tt" file in your project (solution explorer) and say "Run Custom Tool". That's it. This will create constants for almost everything in your MVC project. That means, all views, controllers, actions, javascript files, resources, css files, images, etc are now having constants.. This way you can avoid using the hard-coded strings for views etc in your methods, and can use constants generated by T4MVC. You won't need to worry about path of views etc. T4MVC is very effective and is must for MVC projects.

Change CSS style using drop down menu in MVC

I'm trying to make a drop down menu that has a bunch of items (Amelia, Cerulean, Cosmo, Cyborg, Flatly, Journal). Each of these items represent a css file.
When one of them is selected I want my website to take this selected css file and apply it to the website.
I would like the drop down menu to interact with jquery, meaning when a item is selected jquery takes over and makes a asyn/ajax call to some mvc actionresult.
By the way I'm using MVC 5.
I hope someone can help me sketch the initial groundwork.
I've implemented this in my application.
I'm not sure what to tell you though. It's easier when we have an attempted solution to fix.
Here's an overview of how mine works:
I have created a controller called SharedController. The purpose of it is to contain various actions that render common actions. All of the actions are considered ChildActionOnly.
My _Layout uses RenderAction to render the action NavbarPartial which is in my SharedController.
More importantly the Navbar partial then uses RenderAction to render the action ThemeListPartial. This action is responsible for getting a list of available themes. The list of available themes is determined at applications startup. I've created a ThemeFinder class and ThemeRepository class that are responsible for finding and storing themes. The ThemeFinder finds themes by expressions that you give it. In a new class called App_Start/ThemeConfig I've given it only one expression - "~/Content/themes/{name}.bootstrap.css". This will find all themes with that naming convention in that location.
My razor code will take the ViewModel and display a dropdown menu in the navbar.
To get the themes to change my dropdown menu contains an AJAX link to an action called SaveTheme in ThemeController. This action takes a theme name as a string and tries to save it in a cookie for the user.
If the theme is found and saved successfully, the action responds with a success message.
jQuery then changes the theme by finding the associated link attribute and changing the HREF contents to the new theme. It knows the new theme relative URL because I have it stored in data attributes.
I completed this before I made the switch to AngularJs. The one thing I plan to go back and change is to cut out as much (maybe all) jQuery as possible and replace it with better code.

MVC template from folder other than default (EditorTemplates/DisplayTemplates)?

Can you point MVC to a folder other than the default ones (Views/Shared/EditorTemplates & Views/Shared/DisplayTemplates)? I'd like to either put them in subfolders below those, or in other folders outside the Shared folder.
For example, if I have an editor template under this folder:
~\Views\Order\ProductModel.cshtml
How can I tell my EditorFor template to use this tempalte name?
I tried fully qualifying it, but this doesn't work:
#Html.EditorFor(m => m.ProductModel, #"~\Views\Order\ProductModel.cshtml")
I've tried using forward slashes & backslashes, with/without .chstml, every combination I can think of. I'm beginning to think this isn't supported, but I can't imagine why it wouldn't be.
No, I am afraid you can't do this.
For example, if I have an editor template under this folder
That's no longer an editor template. It's a partial. If you want to share editor templates between different controllers you can simply put them inside the ~/Views/Shared/EditorTemplates folder.
As far as subfolders are concerned then you could do this:
#Html.EditorFor(x => x.Foo, "Order/ProductModel")
which will render the ~/Views/CurrentController/EditorTemplates/Order/ProductModel.cshtml or ~/Views/Shared/EditorTemplates/Order/ProductModel.cshtml editor template.
Old question, but... proper way to add display/editor template for specific controller is to add it in DisplayTemplates or EditorTemplates subfolder.
Assuming that, in your example, you have OrderController, you can simply put display template for your model into sub-folder, like this:
~\Views\Order\DisplayTemplates\ProductModel.cshtml
Then, call display template normally from your view (ex: from `~\Views\Order\Index.cshtml):
#Html.DisplayFor(m => m.MyProductModelProperty)
If you do this:
#Html.EditorFor(x => x.Foo, "Order/ProductModel")
it won't parse Foo as a collection and apply your editor template to each item. It will rather assume that your editor template should be applied to the collection as a whole.
If you want to apply editor template to each item individually, just place it in EditorTemplates folder under your view folder (as it will have precedence) and use the default syntax:
#Html.EditorFor(x => x.Foo)
Of course, the name of the editor template has to match the type of items in your collection.

MVCContrib input builder vs MVC 2 editor templates

Is there any comparison? Pros and cons?
MVC 2 editor template is an evolution of MVCContrib input builder.
It's true that is missing a few things, like Darin Dimitrov said, but It's a fact that it's has a stronger architecture.
Pros:
Have a special folder EditorTemplates under the shared folder AND under the view! (area supported too). In the older MVCContrib input builder you must put the templates in the a shared folder. In order to use it with a special folder you must add a new view engine that will look on this folder.
Cons:
None.
Pros of input builders: they can recognize attributes on the model such as MaximumStringLength and set the maxlength html attribute. They also can recognize the presence of the Required attribute and put the required class on the element.
Pros of editor templates: support of incremental sequencing.

Using JQuery with ASP.NET MVC Framework

I have searched the forum, and google for this topic. Most of the articles are talking about using JSON to call the controller/action on the server and do ajax effect on the result.
I am trying to use some very basic JQuery features, like the JQuery UI/Tabs, and JQuery UI/Block for a dialog window. I cannot get these simple samples to work in my MVC project. Any ideas how I should modify these samples? I only need these basic feature now and I can go from here.
Thanks!
Actually I just got it working. The problem is that I need to modify the path to an absolute path to the view page because the relative path doesn't work with the MVC routes {controller}/{action}/{id}.
Thanks!
For info, re the relative path issue - I discussed this here (the same concept applies to any page, not just master pages). The approach I used is like so:
1: declare an extension method for adding scripts:
public static string Script(this HtmlHelper html, string path)
{
var filePath = VirtualPathUtility.ToAbsolute(path);
return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
}
2: when needed (for example in the <head>...</head>) use this method:
<%=Html.Script("~/Scripts/jquery-1.2.6.js")%>
The advantage of this is that it will work even if the web app is hosted in a virtual directory (i.e. you can't use "/Scripts" because you aren't necessarily at the site root) - yet it is a lot clearer (and less messy) than the full script with munged src, i.e.
<script ... src="<%=Url.Foo(...)%>"></script>
I just implemented the jquery autocomplete textbox in one of my asp.net project. I only had to import the js file and drop some code into my aspx page. Could you be more detailled about what sample you are trying to run?
This is quick response!!
I am trying to run this "Simple Tabs" on this page:
http://stilbuero.de/jquery/tabs/
I think it is the same with this one: http://docs.jquery.com/UI/Tabs
I just copied and pasted the whole thing into my MVC view page, with corrected path to the jquery.js and .css files, but the content in the tabs all show up together (two of them are supposed to be hidden). My understanding is that this simple jquery plugin just show and hide content.
I had the exact same problem with the jquery thickbox plugin, that the item marked as "hidden" (the dialog box) will always show up in my MVC view page.
I can understand some of the MVC+Jquery+json articles, but I don't understand why the hide/show doesn't work.
Thanks!
I just made a walkthrough on how to do this:
http://blogs.msdn.com/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx

Resources