How should I organize my mvc templates?

Posted by CatZ on Stack Overflow See other posts from Stack Overflow or by CatZ
Published on 2010-05-04T09:05:22Z Indexed on 2010/05/04 9:08 UTC
Read the original article Hit count: 242

Filed under:

Hi,

In the new version of MVC we have been given display and editor templates. I was wondering how to make the best possible use of these. I was at first thinking in the line of creating one for a collection and one for an item in the case I want to display either as a list or single but it does not really make sense to me. I'll show you what I mean and please comment if this is wrong. I have trouble figuring it out myself.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DisplayPhoneModel>>" %>
<% foreach (var item in Model){%>
    <%= Html.DisplayFor(x => item) %>
<%}%>

So in my tiny reality that would for every item in the model search for a display template for DisplayPhoneModel. That display template could then simply look like.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DisplayPhoneModel>" %>
<%= Html.LabelFor(x => x.PhoneType) %>&nbsp;<%= Html.LabelFor(x => x.PhoneNumber) %>

So, question is if there is a better way to handle this? Am I at all on the right path?

© Stack Overflow or respective owner

Related posts about asp.net-mvc