您的位置:首页 > 其它

display模版详细介绍

2015-02-27 16:20 281 查看

ASP.NET MVC 2 Templates, Part 4: Custom Object Templates

Series Index

Part 1: Introduction

Part 2: ModelMetadata

Part 3: Default Templates

Part 4: Custom Object Templates

Part 5: Master Page Templates

Customizing Templates

In Part 3, we saw what the default templates would look like if we’d written them as .ascx files. In this blog post, we’ll discuss some of the customizations you can make to the Object templates to enable different features and different displays for your template-based UI.

For these examples, here are the models, controller, and views that we’ll be using.

Models/SampleModel.cs

?
Models/ChildModel.cs

?
Controllers/HomeController.cs

?
Views/Home/Index.aspx

?
Views/Home/Edit.aspx

?

The Default Display

When we show this home controller without any customizations, this is what the details page looks like:



And this is our edit page:



Tabular Layout

One of the more commonly requested layouts is to do a tabular layout inside of the linear name/value, name/value layout that we do by default. Notice that the editor version of this layout also adds asterisks to the label for required fields.

Views/Shared/DisplayTemplates/Object.ascx

?
Which creates this layout:



Views/Shared/EditorTemplates/Object.ascx

?
Which creates this layout:



Shallow Dive vs. Deep Dive

In the screenshots above, ChildModel is showing as “(null value)”. ChildModel is itself a complex model, so it follows the rules for shallow dive vs. deep dive. Before we have a child model object, it’s showing the NullDisplayText as we set in the attribute in the model above.

Notice that even in edit mode above, we can’t edit the child model. That’s because the shallow dive logic prevents us from presenting a recursive editing UI.

If we change the Editor template above and remove the first “if” statement (which is what prevents the deep dive), then the editor will now show us editing fields for the child model:



And now our display shows:



Since we haven’t changed our Object Display template, we still get a shallow dive on this object. Further, it’s showing us the full name because we’ve used the DataAnnotations [DisplayColumn] attribute to say “display this property when showing this complex object in shallow form”. We’ve pointed [DisplayColumn] to a synthesized property called FullName, which we don’t normally show because we’ve annotated it with [ScaffoldColumn(false)].

If we change the Object Display template to do a deep dive, then we would see this:



Wrapping Up

In this blog post, I’ve shown you some of the ways you can customize the Object template to get different displays for your templates. That includes a tabular display instead of a linear display, adding asterisks to field names when fields are required, as well as enabling Deep Dive scenarios for complex objects inside of complex objects. In the next blog post, I’ll examine changing all the templates to enable an entirely different layout system centered around Master pages, inspired by Eric Hexter’s Opinionated Input Builders blog post series.

文章引自:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: