开发者

How should I organize my mvc templates?

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#" I开发者_C百科nherits="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?


No need to foreach manually:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<DisplayPhoneModel>>" %>
<%= Html.DisplayForModel() %>

And the display template:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜