gpt4 book ai didi

webforms - 如何让 DataAnnotations 在 asp.net 4.5 WebForms 中工作?

转载 作者:行者123 更新时间:2023-12-04 20:15:44 28 4
gpt4 key购买 nike

我正在使用带有模型绑定(bind)和 Entity Framework 5 的 .net 4.5 WebForms。

我的网络表单的一部分:

                        <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>

我要绑定(bind)的实体:

    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”,尽管有数据注释。

我做错了什么?

最佳答案

在 Asp.Net 4.5 webforms 中使用模型绑定(bind)的数据注释似乎不能 100% 工作。作为解决方法,我不得不像这样格式化每个页面标记上的显示:

<asp:Literal runat="server" ID="dueDate" Text='<%#Eval("DueDate", "{0:yyyy/MM/dd}") %>'></asp:Literal>

讨厌,但是项目有截止日期并且需要完成。让我们希望微软在下一个版本中解决这个错误。

关于webforms - 如何让 DataAnnotations 在 asp.net 4.5 WebForms 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16943184/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com