5

I have for example a class:

public class Book
{
public int BookId { get; set; }
public string BookName { get; set; }
public string Description { get; set; }
}

and an editor template:

@model MySimpleEditorTemplate.Models.Book

@Html.DisplayFor(p => p.BookId)     @Html.EditorFor(p => p.BookId)
@Html.DisplayFor(p => p.BookName)   @Html.EditorFor(p => p.BookName)
@Html.DisplayFor(p => p.Description)    @Html.EditorFor(p => p.Description)

I can use the editor template like this:

@Html.EditorFor(model => model.Book) 

However what if I want to have two editor templates or two display templates and use one or other for the same class? Is this possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

2 Answers2

14

YES, you can have the "default" one, with its name Book.cshtml... and this one is triggered every time you use EditorFor.

You can have another editor template for Book, let's say calling it BookTheOtherWay.cshtml and there you place your "other editor view". Now, when using EditorFor, you just need to pass the template name as other parameter in the EditorFor template.

@Html.EditorFor(model => model.MyBook, "BookTheOtherWay" )

This works the same way for DisplayTemplates and the DisplayFor helper.

@Html.DisplayFor(model => model.MyBook, "BookTheOtherWay" )
Romias
  • 13,783
  • 7
  • 56
  • 85
4

Yes

public static MvcHtmlString EditorFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    string templateName
)

http://msdn.microsoft.com/en-us/library/ff406506.aspx

"If a template whose name matches the templateName parameter is found in the controller's EditorTemplates folder, that template is used to render the expression. If a template is not found in the controller's EditorTemplates folder, the Views\Shared\EditorTemplates folder is searched for a template that matches the name of the templateName parameter. If no template is found, the default template is used."

LostInComputer
  • 15,188
  • 4
  • 41
  • 49
  • I think I understand it. What does it mean about "the controller's editortemplates folder". I am wondering where I should be storing my editor template and I never heard of that as a location? Could I maybe keep just the same name but have a different template in different controller editortemplates folders? – Samantha J T Star Nov 19 '11 at 02:48
  • You can have templates for any model you want. For example, for a model named `MyModel` just create a file named MyModel.cshtml. Then, when you call `EditorFor()` MVC first looks for it on \Views\CurrentController\EditorTemplates\, if nothing is found it looks on \Views\Shared\EditorTemplates\ and finally, it displays the default one if nothing is found. Same can be done to `DisplayFor()` helper but folder is DisplayTemplates\ . – Joao Nov 19 '11 at 03:50
  • When I say model, it can be any class type. You can do it for `string` for example. Just create a string.cshtml file. Now every time you use `EditorFor(model => model.MyStringProperty) it uses your template and not the default one. – Joao Nov 19 '11 at 03:55