zoukankan      html  css  js  c++  java
  • asp.net mvc 2 DisplayTemplates 的使用

    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="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>
     
     
    ------------------------------------------------------------------------------------
    作者:王继坤

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    ------------------------------------------------------------------------------------
  • 相关阅读:
    洛谷 P5110 块速递推
    洛谷 P3868 [TJOI2009]猜数字
    Codeforces 343D Water Tree
    Codeforces 915E Physical Education Lessons
    洛谷 P2787 语文1(chin1)- 理理思维
    洛谷 P4344 [SHOI2015]脑洞治疗仪
    洛谷 P3338 [ZJOI2014]力
    【模板】珂朵莉树(ODT)(Codeforces 896C Willem, Chtholly and Seniorious)
    【模板】FFT
    Solution of CF911G Mass Change Queries
  • 原文地址:https://www.cnblogs.com/wangjikun3/p/1768850.html
Copyright © 2011-2022 走看看