I've got a couple of questions about the naming convention for the DisplayTemplates and EditorTemplates in MVC 2.
If for example I have a customer object with a child list of account how do I:
Create a display template for the list of accounts, what is the file called?
When I'm doing a foreach( var c in Model.Accounts )
how do I call a display temple while in the foreach loop? When I do Html.DisplayFor( x => x )
inside the foreach x
is the model and not in this case c
.
Thanks in advance.
Yeah, this is one of my favorite features, but it's a bit confusing for some things.
So, creating a template for any class, the name is based on the type's Name property, so for example if you do a <%= model.GetType().Name %> in your view, you can see what I mean. As an example, if your list of accounts is an IList, then the call to .Name on the type would return List`1
. That's a legal file name, so if you put List`1.ascx
in your DisplayTemplates folder, it'll find it and use it. from what I can tell, it won't traverse the class hierarchy, though, so for example if you put an IEnumerable`1.ascx file in there, it won't find it.
To display in a loop, you need to pass in the item variable into the lambda, so in your example:
<% foreach (var c in Accounts){ %>
<li><%= Html.DisplayFor(x => c) %></li>
<%}%>
Hope that helps. Paul
I realize this question is a bit old, but at least in MVC 3 when you want to have a custom display/edit template using a list you can pass in the name of the template:
Html.DisplayFor(model => model.Account, "AccountList")
then the name of your template would be "AccountList".
Brad Wilsons blog here http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html has an example about DisplayTemplates and EditorTemplates for Custom Objects, and it discusses issues which may be helpful in resolving your question.
Regarding your first question, it is important that your template resides in a folder which the system looks for matching names in, e.g.
~/Views/ControllerName/DisplayTemplates/TemplateName.aspx & .ascx
~/Views/Shared/DisplayTemplates/TemplateName.aspx & .ascx
(Replace DisplayTemplates with EditorTemplates for the search paths for editor templates.)
The following template names are tried in order:
I'm not sure about an answer for your second question. Is it right to assume that var c is actually effectively Account c? And when you are writing Html.DisplayFor(x => x)
you might as well be writing Html.DisplayModelFor(c => c)
? Or do you want to have `Html.DisplayFor(x => x.AccountName), for instance?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With