MVCContrib input builder vs MVC 2 editor templates - asp.net-mvc

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.

Related

Kendo editor templates is not found when placing inside an area

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.

When should we use Html Helpers, Razor Helpers or Partial Views?

These three different features exist in the Razor view engine and can be used to achieve similar results. In the end all three of them just render pieces of HTML code, but the way to define and use them is fairly different. I know that:
Html Helpers are created as extension methods for the HtmlHelper class. They frequently use the TagBuilder class to generate some HTML and always should return an IHtmlString.
Razor Helpers (#helper methods) can be defined locally (in the same razor file) or globally (in the App_Code directory). They are small snippets of HTML code that can be reused exclusively in Razor files.
And finally, Partial Views are just regular view files that can be included in other view files using the #Html.Partial helper.
My question is:
Is there a specific scenario for each one of these features? Or it comes down to different flavors to achieve the same result?
HTML Helpers are for reusable components. e.g. WebGrid, Pager, etc. These are distributed as assemblies and have no dependency on Razor. Choose this if:
Functionality is truly reusable and applicable to any application
You don't want people to modify it, or want to version it
Partials Views are a way to split large views into smaller parts to keep things more manageable. They are also useful for reusability that is specific to your application. These are located by the view engine, so you can have the same partial defined in different places (e.g. Views/Shared), allowing you to customize per controller, area or display mode. Choose this if:
Functionality is application-specific
Want to customize per controller, area or display mode
Local Helpers are a way to execute the same template many times, without having to repeat yourself. You can also use it to break views into parts to avoid deep nesting, but keeping everything in the same file. Choose this if:
Functionality is view-specific
Application Helpers (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:
Prefer Razor over TagBuilder
Don't mind distributing files instead of assemblies
Prefer type-safe method-call syntax instead of #Html.Partial(name)

Migrate to Razor

Is it possible to migrate an existing ASP.NET MVC3 project using the standard (webforms-like) template engine to Razor? How?
Thanks
It should be possible.
Rename your *.aspx to *.cshtml. Firstpay attention to control directives, for example
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.TheModel>
now becomes
#model MvcApplication1.Models.TheModel
Now examine your views carefully and replace all tags to razor-style tags (<% %> -> #)
Note that you don't have to migrate all your views - it's perfectly legal to mix the two view engines in a single MVC application so you can plan your migration over time.

Share a razor declarative helper between multiple mvc web projects

Let's say I have 2 MVC web projects (web1 and web2) and 1 project containing shared views (common) (using the razorgenerator of David Ebbo)
web1 and web2 both have a test.cshtml file. Several blocks of code in both test.cshtml files are exactly the same.
I'm trying to find out if it's possible to share a declarative helper (#helper) between several cshtml files which are in DIFFERENT projects. So putting a cshtml file in my App_Code does not help (I would need to have 1 in each web project, which is obviously not what I want).
I know I could create a bunch of shared partial views in my 'common' project, but it seems kinda overhead to create 20 cshtml files that each contains a very small portion of html.
I know I can create a standard helper method (static string GenerateAPieceOfHtml(this HtmlHelper helper, ....)), but there I loose the ease of writing html as you can do it in a cshtml file.
For a short while I thought I bumped into an answer that would allow me to do it. But as I wrote in a comment, that code did not compile for me.
I hope my question is clear :)
[Update]
As csharpsi asks in a comment.. I did try out the code from the other post, but it did not spit out any HTML for me. Since I started to think that that answer should probably do the trick since it has 13 upvotes, I decided to give it a second try..
Again I didn't get any output, but then I tried it a little bit different.. and success!
I was trying this (which doesn't spit out any html on the page):
#{ new Test().DoSomething(Model); }
This is the version that DOES WORK:
#{
var html = new Test().DoSomething(Model);
#html
}
Other version that works:
#(new Test().DoSomething(Model))
What should I do with this question? Delete it? Write an answer myself?
Why are you trying to use razor helper for this anyway ? Razor helpers are one-particular-viewengine hack, your application shouldnt rely on them on many places (even amongst different websites). In this case, be sure to use standard MVC way - HTML helper. These you can easily share between websites, for example you can make your own class library full of them.

Module Localization in DNN

I don't know much about the localization process in DNN. The question is that how can you localize a new module?
Is it possible to include localization files with every module separately? What solutions can you come up with?
Localization of a module is pretty easy thanks to DotNetNuke.
Wherever your .ascx (View) file is, the App_LocalResources folder should always accompany it, on the same level. There should also be a corresponding .ascx.resx file in that folder.
view.ascx
App_LocalResources
- view.ascx.resx
Once you have that structure in your module. DNN will pick the file up immediately.
To use that resource strings in the resx. Simple tack on the ResourceKey property to the end of your asp controls. e.g.
<asp:Label ID="lblExample" runat="server" ResourceKey="lblExample" />
You should have a lblExample.Text in your resx file which matches up with that label. Note that it adds .Text to it automatically.
If it's not showing up, there are a few things to check
LocalResourceFile property in code. What location is it pointing to?
set ShowMissingKeys=true in web.config and you'll see what resource strings you're missing.
Please find this document. I am not sure if it covers your questions and how localizing DotNetNuke modules is different from other Asp.Net applications but please try it out.
If I may suggest something, I would add more tags in the future (like C# for example), it will be visible to broader audience which may result in better answers.
Simply create a folder called "App_LocalResources" on the same level as your .ascx view files in your project. For each file that you want localized, simply add a .resx file with the same name as the view (including the .ascx extension).
Resx Name Example:
"View.ascx.resx"
Using localistion is really easy after that. Simply set the Resource Key property of whichever controls you want to pull from your resx file to a meaningful name
Example:
<dnn:Label id="lblName" ResourceKey="lblName" runat="server" />
Resx File:
"lblName.Text" will assign to the Text property of the label
"lblName.Help" will assign to the DNN Tooltip property if you are using dnn:Labels like above
If you want to start using DNN Labels simply put this tag at the top of your page.
<%# Register TagPrefix="dnn" Assembly="DotNetNuke.Web" Namespace="DotNetNuke.Web.UI.WebControls" %>
<%# Register TagPrefix="dnn" TagName="Label" Src="~/controls/LabelControl.ascx" %>
Another handy method available is:
LocalizeString("key")
It will pull from your resource file and it quite handy when working with things like email templates.

Resources