How #Html.EditorFor resolve or point to right template - asp.net-mvc

just read the article for how to work with template for re-usability from this url http://www.iminfo.in/post/mvc-multiple-checkboxes-select-in-html-table-bootstrap
this article show just we need to create a template file in editor template folder with viewmodel name.
i just like to know how MVC #Html.EditorFor can point or pickup to right template file. need some guide line like how MVC resolve right template to pick up. thanks

By default templates are resolved by data type so if you use
#Html.EditorFor(model => model.ProductViewModel)it will use EditorTemplates/ProductViewModel.cshtml template.
However if you need to use a different template you can use an override of #Html.EditorFor(model => model.ProductViewModel, "yourTemplateName") that accepts template name.
Additional option is to set a template by using UIHint attribute on the property for example:
public class ComplexModel
{
//will search for EditorTemplates/SomeProductModelTemplate.cshtml
[UIHint("SomeProductModelTemplate")]
public ProductViewModel ProductModel {get;set;}
}

The template is resolved by data type. In your example you have editor templates folder with a ProductViewModel template. If you then create a view under the home directory using the EditorFor method with a ProductViewModel type then MVC will resolve the custom editor template. Similarly, if you created a DateTime.cshtml file under editor templates then that would be used for EditorFor method calls with a DateTime parameter.
#Html.EditorFor(model => model.ProductViewModelItem) //will use the custom template
#Html.EditorFor(model => model.StringItem) //will not use the custom template
You can also do the same with DisplayTemplates for DisplayFor or any of the other Html "for" helpers.

Related

How to show the value of model in Kendo Custom Editor Template?

I'm using a scheduler of Kendo UI for MVC. I created a Custom Editor Template. It works fine. When click the schedule, it pops a windows and show the info.
I added some properties in the model, the value passed to the editor template. Now, the question is:
How can I show the added properties (just as value, and I do not want to change it) in the editor template popup?
I found that if I use a textbox:
#(Html.TextBoxFor(model => model.Role, new { #class = "k-textbox" }))
It shows a textbox and shows the correct value of Role. However, if I use
<div>#(Model.Role)</div>
It shows blank, just as the value is NULL.
I actually want to show several added properties as a sentence, i.e. I added Role and UserName and I ant to show something like:
UserNameValue has Role of RoleValue
Anyone knows how to do it?
I use this as sample in the code:
http://www.telerik.com/support/code-library/custom-editor
Thanks
Your solution is to put this line because the editor template is binded by mvvm
<div data-bind="text: Role"></div>
Docs
In the code: model => model.Role model is just an alias, not an actual object. You could replace it with m => m.Role and it would function the same.
When you write #Model in your view you are using the object of ContractViewModel which is passed from Controller action, if it is not passed from View it can be null and accessing any property of Model can throw Null Reference Exception and writing Model.Contractors you basically mean ContractViewModel.Contractors
See this post: mvc uppercase Model vs lowercase model

Is there any way to create a custom MVC Razor element like "text"?

I want to create a custom razor tag like <text></text> to decide what to do with the html code inside of it. Is there any way to create razor elements like <text></text> element and add it to the razor engine?
I don't want to create any HtmlHelpers for this.
For Examle:
<WYSYWIG>
Hello There!
</WYSYWIG>
or
<WeatherChart City="NY">
</WeatherChart>
Explanation:
Well the idea is to have server tags to be translated (Parsed) to html codes by the attributes given to them. This kind of codes helps junior developers not to be involved with the complexity of controls.
The closest thing to what you are describing is to create display or editor templates. You can then define a template for a model and use it with #Html.DisplayFor() in the view.
Here is a good blog post to get you started aspnet mvc display and editor templates and a quick overview of the structure below.
Example
Model - WeatherChartModel.cs
public class WeatherChartModel
{
}
Display template - WeatherChart.cshtml
<div class="weather-chart">
// Some other stuff here
</div>
View - Index.cshtml
#model WeatherChartModel
#Html.DisplayForModel() // This will output the template view for the model
In order to create custom element handling in razor, such as <text>, you'd need to implement a custom System.Web.Razor.dll (which is responsible for parsing the document). Specifically, the class you're looking to re-implement would be the System.Web.Razor.Parser.HtmlMarkupParser.
However, I don't believe this is necessary given how flexible the framework itself is. If you're looking to keep things modular, have a look at either using DisplayTemplates/EditorTemplates or consider writing your own extension method. For example, either of the following would be more ideal:
#* TextField is decorated with UIHint("WYSIWYG"), therefore
calling ~/Views/Shared/EditorTemplates/WYSIWYG.cshtml *#
#Html.EditorFor(x => x.TextField)
#* WeatherField is decorated with UIHint("WeatherChart"), therefore
calling ~/Views/Shared/DisplayTemplates/WeatherChart.cshtml *#
#Html.DisplayFor(x => x.WeatherField)
Alternatively:
#* Custom extension method *#
#Html.WysiwygFor(x => x.TextField)
#* Another custom extension method *#
#Html.WeatherChartFor(x => x.WeatherField)

How can I use default MVC editor if i have defined editor template for particular type?

How can I use default MVC editor if i have defined editor template for particular type?
For instance I have editor for string type("string.cshtml") in "Editors" folder. When I use
#Html.EditorFor(x => x.MyStringField)
it uses my template("string.cshtml").
But I have area in my application and I want use there default MVC editor template for string type. Is it possible?
You should place your editor template in folder Views\Shared\EditorTemplates in your area or in your root folder.
If you want, you can set editor template for current property:
#Html.EditorFor(x => x.MyStringField, "editorTemplateName");
In this case you should name editor template custom name (not string).

Using EditorFor template for DisplayFor in MVC4?

I'm using an Editor Template to make an Html.EditorFor(property) in my viewModel's view. There's a different .cshtml file containing "#Html.EditorFor(property)".
Now, depending on the value of a property of my viewModel, I need to display a DisplayFor instead.
I tried doing this by adding some conditional logic in my EditorTemplate but can't seem to access the properties of my viewModel from there (since the editor template is using #model.someOtherModel and not #viewModel). So if I can say something like
// Razor
if(true)
{ EditorFor(property) }
else {DisplayFor(property)}
in my viewModel's view, that would work. I just don't know how to define a "Display Template" for my object, in the same way I defined an Editor Template.
Another solution might be accessing the viewModel data from the Editor template... is this possible?
In Razor, you should be using something like the following syntax:
#if (condition) {
#Html.EditorFor(modelItem => model.property)
} else {
#Html.DisplayFor(modelItem => model.property)
}
Are you getting a specific error you can share?

How can I define the location of a custom editor template when using MVC areas?

It is my understanding that the location is:
~/Views/Shared/EditorTemplates/ViewModelName
However I have many Views folders using areas. Can I define the file to use with some parameter of the call to the
#Html.EditorFor( ...
Those are default lookup paths which RazorViewEngine will search for editor template, in this order:
"~/Areas/{area}/Views/{controller}/EditorTemplates/{templateName}.cshtml",
"~/Areas/{area}/Views/Shared/EditorTemplates/{templateName}.cshtml",
"~/Views/{controller}/EditorTemplates/{templateName}.cshtml",
"~/Views/Shared/EditorTemplates/{templateName}.cshtml",
If not specified, templateName value defaults to object type (in your case 'ViewModelName'). If template with this name is not found by MVC will fall back to resolve rendering using known build-in templates (int, string, collection, object, etc).
You can specify template name to override defaults:
#Html.EditorFor(m => m.MyDate, "_MyTemplate")
You can also specify relative paths:
#Html.EditorFor(m => m.MyDate, "../_MyTemplate")
You cannot specify full paths in any form (ex:"~/Views/Custom/EditorTemplates/ViewModelName") and you should never specify extension in template name (ex: '_MyTemplate.cshtml', or '_MyTemplate.vbhtml')!
You could pass the location of the template as second argument.
#Html.EditorFor(x => x.Foo, "~/Views/Custom/EditorTemplates/ViewModelName.cshtml")
This being said I would avoid doing this and stick to the conventions. This means that if you want to use some editor template from outside the area where it is defined then you probably didn't define this template at the right place and should move it in the Shared folder.

Resources