How to get DataAnnotations to work in asp.net 4.5 webforms?

I am using .net 4.5 WebForms with model binding and Entity Framework 5.

Part of my web form:

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

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

Date fields are displayed in viewing mode as well as in editing mode as "2013/01/01 12:00:00 AM", despite data annotations.

What am I doing wrong?

+3
source share
2 answers

It seems that data annotations with model binding in Asp.Net 4.5 web formats do not work 100%. As a job, I had to resort to formatting the display on each page layout as follows:

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

, . , Microsoft .

0

, 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, Mode = "ReadOnly" ItemTemplate. EF6, . , ole TextBox, aspx , .

0

All Articles