如何使DataAnnotations在asp.net 4.5 WebForms中工作? [英] How to get DataAnnotations working in asp.net 4.5 WebForms?
问题描述
我正在使用带有模型绑定和实体框架5的.net 4.5 WebForms.
I'm using .net 4.5 WebForms with Model Binding and Entity Framework 5.
我的网络表单的一部分:
Part of my webform:
<asp:ListView ID="MenteeModuleList" ItemPlaceholderID="itemPlaceHolder"
OnCallingDataMethods="MenteeModuleList_CallingDataMethods"
ItemType="PFA_Mentorship.BLL.MenteeModuleBL+MenteeModuleGrid"
DataKeyNames="MenteeModuleID"
SelectMethod="GetMenteeModulesGrid"
InsertItemPosition="LastItem"
InsertMethod="InsertMenteeModule"
runat="server">
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<th id="Th1" runat="server">Module</th>
<th id="Th2" runat="server">Start Date</th>
<th id="Th3" runat="server">Due Date</th>
<th runat="server"></th>
</tr>
<tr runat="server" id="itemPlaceHolder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="tr1" runat="server">
<td runat="server">
<asp:Label runat="server" ID="moduleName" Text="<%#: BindItem.ModuleName %>"></asp:Label>
</td>
<td id="tr2" runat="server">
<asp:Label runat="server" ID="startDate" Text="<%#: BindItem.StartDate %>"></asp:Label>
</td>
<td id="td3" runat="server">
<asp:Label runat="server" ID="dueDate" Text="<%#: BindItem.DueDate %>"></asp:Label>
</td>
<td runat="server">
<asp:LinkButton runat="server" ID="Edit" CommandName="Edit" CommandArgument="<%#: BindItem.MenteeModuleID %>" Text="Edit" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
</InsertItemTemplate>
<EditItemTemplate>
<tr>
<td id="Td1" runat="server">
<asp:Label ID="moduleName" runat="server" Text="<%#: BindItem.ModuleName %>"></asp:Label>
</td>
<td id="tr2" runat="server">
<asp:TextBox ID="startDate" runat="server" Text="<%#: BindItem.StartDate %>"></asp:TextBox>
</td>
<td id="td3" runat="server">
<asp:TextBox ID="dueDate" runat="server" text="<%#: BindItem.DueDate %>"></asp:TextBox>
</td>
<td>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
我绑定的实体:
public class MenteeModuleGrid
{
public int MenteeModuleID { get; set; }
public string ModuleName { get; set; }
public int MenteeID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public System.DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public System.DateTime DueDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
public Nullable<System.DateTime> CompletedDate { get; set; }
}
尽管有数据注释,日期字段仍以查看模式和编辑模式显示为"2013/01/01 12:00:00 AM".
The date fields are displaying in view mode as well as edit mode as "2013/01/01 12:00:00 AM" despite the data annotations.
我在做什么错了?
推荐答案
Asp.Net 4.5 Webforms中具有模型绑定的数据注释显然无法100%正常工作.作为一种变通办法,我不得不采取如下方式格式化每个页面标记上的显示:
It surely seems that data annotations with model binding in Asp.Net 4.5 webforms does not work 100%. As a work around I had to resort to formatting the display on each page's markup like this:
<asp:Literal runat="server" ID="dueDate" Text='<%#Eval("DueDate", "{0:yyyy/MM/dd}") %>'></asp:Literal>
令人讨厌,但项目有到期日,需要完成.希望微软能够在下一版本中解决该错误.
Nasty, but projects have due dates and need to be completed. Lets hope Microsoft sorts this bug out in the next release.
这篇关于如何使DataAnnotations在asp.net 4.5 WebForms中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!