惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
U
Unit 42
WordPress大学
WordPress大学
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
V
V2EX
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 王继坤

DataTable 递归 简单的程序,来实现无限级列表 结合 jquery.table.js 实现 asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题 asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T> asp.net mvc 2 简简单单做开发 自定义Controller基类 - 王继坤 asp.net mvc 2 简简单单做开发 自定义DropdownList控件 asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面 FcDigg 源码 asp.net 3.5 linq to sql 扩展方法 linq to access 简单实现 实例demo linq to access 简单实现 asp.net mvc 随想 asp.net mvc ajax实现1 asp.net ajax fccms 小型简单个人blog源码 FCKEDITOR中文使用说明 js调用 dotfuscator.exe 采用线程生成静态页面 新的开始--- gridview 实现多字段综合查询
asp.net mvc 2 DisplayTemplates 的使用
王继坤 · 2010-07-01 · via 博客园 - 王继坤

asp.net mvc 2 官方给的例子有些简单,主要介绍了新的功能。下面主要介绍下DisplayTemplates 给我们带来的实用的功能,可以自定义字段显示的方式,按类型分:String、Boolean、Decimal。按显示的方式:EmailAddress、Html、Url、HiddenInput。还可以自定义字段的显示 如:DropDownList。可以扩充类型的显示 如:DateTime,只要和字段的类型相同都可以直接使用,而不用绑定。下班一个简单的MetaData的例子。它可以扩充数据模型,定义一些自定义的内容。

 1  [MetadataType(typeof(Article_MetaData))]
 2     partial class Article
 3     {
 4 
 5     }
 6     public class Article_MetaData
 7     {
 8        
 9         [ScaffoldColumn(false)]
10         public int Id { getset;}
11         [DisplayName("标题")]
12         [Required]
13         [SearchFilter]
14         public string title { getset; }
15 
16         [Display( Name="",Order=12)]
17         [Required]
18         [SearchFilter]
19         [DisplayName("栏目")]
20         [DropDownList("Category""Id""Name")]
21         public int Cid { getset; }
22         [DisplayName("模型")]
23         [ScaffoldColumn(false)]
24         public int ModeId { getset; }
25         [DisplayName("排序")]
26         [Required]
27         public int OrderID { getset; }
28         [DisplayName("时间")]
29         [Required]
30         public DateTime CreateTime { getset; }
31 
32         [DisplayName("内容")]
33         [DataType(DataType.Html)]
34         public string Cont { getset; }
35     }

关于MetaData的详细内容可以参考msdn上的介绍。mvc 对MetaData内的部分内容支持不是太完善,有些内容还需要自己来扩展。如[Display()]就不能使用,如果使用的话,你必须自定义 ModelMetadataProviders。通过它,你可以实现很多功能。

 DisplayTemplates 文件夹内的自定义控件只针对html.display() 使用。下边说下,我使用的自定义表格,先将表格用分头部,和主体内容两部分,分别是 header、Rows。

header.ascx代码

 1 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
 2 <script runat="server">
 3     bool ShouldShow(ModelMetadata metadata) {
 4         return metadata.ShowForDisplay
 5             && metadata.ModelType != typeof(System.Data.EntityState)
 6             && !metadata.IsComplexType
 7             && !ViewData.TemplateInfo.Visited(metadata);
 8     }
 9 </script>
10 <% if (Model == null) { %>
11     <%= ViewData.ModelMetadata.NullDisplayText %>
12 <% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
13     <%= ViewData.ModelMetadata.SimpleDisplayText %>
14 <% } else { %>
15 
16     <% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) { %>
17         <% if (prop.HideSurroundingHtml) { %>
18             <%= Html.Display(prop.PropertyName) %>
19         <% }
20            else if (prop.DataTypeName != "Html" && prop.DataTypeName != "MultilineText" && prop.DataTypeName != "Text")
21            { %>
22             <% if (!String.IsNullOrEmpty(prop.GetDisplayName())) { %>
23                 <th><%= prop.GetDisplayName() %></th>
24             <% } %>
25              
26         <% } %>
27     <% } %>
28   
29 
30 <% } %>

rows.ascx 代码

代码

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    
bool ShouldShow(ModelMetadata metadata)
    {
        
return metadata.ShowForDisplay
            
&& metadata.ModelType != typeof(System.Data.EntityState)
            
&& !metadata.IsComplexType
            
&& !ViewData.TemplateInfo.Visited(metadata);
    }
</script>
<% if (Model == null) { %>
    
<%= ViewData.ModelMetadata.NullDisplayText %>
<% } else if (ViewData.TemplateInfo.TemplateDepth > 1) { %>
    
<%= ViewData.ModelMetadata.SimpleDisplayText %>
<% } else { %><% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => ShouldShow(pm))) {
        
%>
        
<% if (prop.HideSurroundingHtml) { %>
            
<%= Html.Display(prop.PropertyName) %>
        
<% } else if(prop.DataTypeName!="Html"&&prop.DataTypeName!="MultilineText"&&prop.DataTypeName!="Text") { %>
           
            
<td><%= Html.Display(prop.PropertyName) %></td>
        
<% } %>
    
<% } %>
    
<% } %>

调用表格使用的代码

代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Import Namespace="mvc.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
<div id="headImg">
        
<img src="/content/adminimages/01.gif" alt="" /><%:ViewData["NavTitle"]%>
        
</div>
        
<div id="cont2">
        
<div class="form clearfix">
   
    
<form id="form1" method="get">
    
<div id="Search" class="clearfix">
    
<%=Html.DisplayFor(m => ViewData["searchModel"], "tool","")%>   <li class="add">
        
<%: Html.ActionLink("添加""Add"%>
    
</li>
    
</div>
    
</form>
   
<table class="tb">
    
<% int i = 0foreach (var art in Model)
       {
           i
++;
           
if (i == 1)
           {  
           
%>
  
<tr  class="tbhead">
  
<%=Html.DisplayFor(m => art, "header""di")%>
  
    
<th>操作</th>
  
</tr>
  
<%%>
  
<tr>
  
<%=Html.DisplayFor(m => art, "rows""di")%>
  
<td style="width:60px">
   
<%: Html.ActionLink("编辑""Edit"new { id = art.Id })%>  <%: Html.ActionLink("删除""Delete"new { id = art.Id }, new { onclick = "return confirm('你确定要删除吗?')" })%>
  
</td>
  
</tr>
  
<%%>
  
</table>
    
    
<div class="page">
     
<%=ViewData["page"%>
    
</div>
    
</div>
    
</div>
</asp:Content>