【问题标题】:How to get DataAnnotations working in asp.net 4.5 WebForms?如何让 DataAnnotations 在 asp.net 4.5 WebForms 中工作?
【发布时间】:2013-06-01 08:17:26
【问题描述】:

我正在使用带有模型绑定和实体框架 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>

我绑定到的实体:

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

我做错了什么?

【问题讨论】:

  • 如果您将鼠标悬停在 DisplayFormat Annotation 上,我认为它不是框架的错误,它清楚地指出这仅适用于编辑模式下的动态字段,例如

标签: webforms data-annotations model-binding


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我同意 WebForms 中的某些数据注释功能不像在 MVC 中那样工作。

    我一直在使用 DynamicField 控件代替文本框来解决这个问题:

    <asp:TextBox ID="dueDate" runat="server" text="<%#: BindItem.DueDate %>"></asp:TextBox>
    

    变成

    <asp:DynamicControl ID="dueDate" runat="server" DataField="DueDate" Mode="Edit" />
    

    ...至少在 EditTemplate 中,因此在 ItemTemplate 中类似地更改为 Mode="ReadOnly"。我只能用 EF6 对此进行测试,因为我在几年后发布。不幸的是,它不适用于良好的 ole' TextBox 控件,如果不编辑它们,使用现有的 aspx 文件库就变得不那么愉快了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2020-06-20
      • 2016-01-15
      • 2011-10-19
      • 2011-09-23
      • 1970-01-01
      相关资源
      最近更新 更多