问题
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>
My entity I'm binding to:
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; }
}
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.
What am I doing wrong?
回答1:
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.
回答2:
I agree that some of the data annotation functionality in WebForms does not work like in MVC.
I've been using a DynamicField control in place of text boxes for this issue:
<asp:TextBox ID="dueDate" runat="server" text="<%#: BindItem.DueDate %>"></asp:TextBox>
becomes
<asp:DynamicControl ID="dueDate" runat="server" DataField="DueDate" Mode="Edit" />
...at least in the EditTemplate, so similarly change to Mode="ReadOnly" in the ItemTemplate. I was only able to test this with EF6, as I'm posting a few years later. It's unfortunate though that it doesn't work for the good ole' TextBox control, making working with a base of existing aspx files less joyful without editing them all.
来源:https://stackoverflow.com/questions/16943184/how-to-get-dataannotations-working-in-asp-net-4-5-webforms