开发者

Why would you want to use a MVC 3 Template?

A few weeks ago, I figured out how to write my own DisplayTemplates and EditorTemplates. I do find them useful as I can write something as this: @Html.DisplayForModel("my_awesome_template") which will display whatever is in the "my_awesome_template" and use the current Model in the View. Fantastic right?

Well, I have written my views in so many different ways, that now I am ending right back to way I first wrote them: Just straight in the View with no partials or templates, and using ViewModels. Why? Because I lack ultimate flexibility and lose consistency. For example: If I had in my DisplayTemplate, @Model.HamSandwhichText, since this is in my template, I can only move it around inside my template and not in the actual View. Technically I could use it directly in the View, but at that point I just broke consistency. This means that the View and the Template both have access to the same thing. I don't like this. I'm assuming since I am using ViewModels I will not have to worry so much about templates.

But, what is the real reason to use a template? I do notice it makes displaying lists a little bit cle开发者_StackOverflow社区aner and easier; but I still don't like it.


There are two reasons to use editor/display templates:

1) Syntax sugar. It helps make your html easier to read by isolating specific chunks of code. Most of my views/layouts are very lightweight due to my usage of:

  • partial views
  • templates
  • sections

2) It makes code more maintainable. Separating your html into concise chunks of code with a purpose can really help you focus on what you're working on. I can easily add/remove/replace entire sections of code with ease.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜