asp.net mvc 2 DisplayTemplates 的使用
asp.net mvc 2 官方給的例子有些簡(jiǎn)單,主要介紹了新的功能。下面主要介紹下DisplayTemplates 給我們帶來的實(shí)用的功能,可以自定義字段顯示的方式,按類型分:String、Boolean、Decimal。按顯示的方式:EmailAddress、Html、Url、HiddenInput。還可以自定義字段的顯示 如:DropDownList。可以擴(kuò)充類型的顯示 如:DateTime,只要和字段的類型相同都可以直接使用,而不用綁定。下班一個(gè)簡(jiǎn)單的MetaData的例子。它可以擴(kuò)充數(shù)據(jù)模型,定義一些自定義的內(nèi)容。
?
?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?{?get;?set;}
11?????????[DisplayName("標(biāo)題")]
12?????????[Required]
13?????????[SearchFilter]
14?????????public?string?title?{?get;?set;?}
15?
16?????????[Display(?Name="",Order=12)]
17?????????[Required]
18?????????[SearchFilter]
19?????????[DisplayName("欄目")]
20?????????[DropDownList("Category",?"Id",?"Name")]
21?????????public?int?Cid?{?get;?set;?}
22?????????[DisplayName("模型")]
23?????????[ScaffoldColumn(false)]
24?????????public?int?ModeId?{?get;?set;?}
25?????????[DisplayName("排序")]
26?????????[Required]
27?????????public?int?OrderID?{?get;?set;?}
28?????????[DisplayName("時(shí)間")]
29?????????[Required]
30?????????public?DateTime?CreateTime?{?get;?set;?}
31?
32?????????[DisplayName("內(nèi)容")]
33?????????[DataType(DataType.Html)]
34?????????public?string?Cont?{?get;?set;?}
35?????}
?
關(guān)于MetaData的詳細(xì)內(nèi)容可以參考msdn上的介紹。mvc 對(duì)MetaData內(nèi)的部分內(nèi)容支持不是太完善,有些內(nèi)容還需要自己來擴(kuò)展。如[Display()]就不能使用,如果使用的話,你必須自定義 ModelMetadataProviders。通過它,你可以實(shí)現(xiàn)很多功能。
?DisplayTemplates 文件夾內(nèi)的自定義控件只針對(duì)html.display() 使用。下邊說下,我使用的自定義表格,先將表格用分頭部,和主體內(nèi)容兩部分,分別是 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>
????????<%?}?%>
????<%?}?%>
????
<%?}?%>
?
調(diào)用表格使用的代碼
代碼 <%@?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?=?0;?foreach?(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('你確定要?jiǎng)h除嗎?')"?})%>
??</td>
??</tr>
??<%}?%>
??</table>
????
????<div?class="page">
?????<%=ViewData["page"]?%>
????</div>
????</div>
????</div>
</asp:Content>
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wangjikun3/archive/2010/07/01/1768850.html
總結(jié)
以上是生活随笔為你收集整理的asp.net mvc 2 DisplayTemplates 的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle中用For Loop 替代C
- 下一篇: PetShop之ASP.NET缓存(转载