Thursday, 21 February 2013

ASP.NET Simple Example - Model Binding Entity Framework with ListView


Watch Online


1. For purpose of this tutorial I have simple database with table called GroupMaster.
Table has list of different groups:

2. First  create new project in Visual Studio 2012.  Add ADO.NET Entity Data Model to it and select GroupMaster table (all details in video).
3. Add new web page.
4. Add ListView to the page
        <asp:ListView ID="ListView1" runat="server" ItemType="WebApplication14.GroupMaster"
            SelectMethod="GetGroups">
            <ItemTemplate>
                <a href="/AnotherPage.aspx?id=<%#: Item.GroupId %>"><%#: Item.GroupName %></a>

            </ItemTemplate>
        </asp:ListView>
5. Ensure that ItemType is set to WebApplication14.GroupMaster (you may have to rebuild project to get intellisense)
6. Ensure that SelectMethod is set to GetGroups.
7. Implement GetGroups method:
        public List<GroupMaster> GetGroups()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                var result = db.GroupMasters.ToList();
                return result;
            }
        }
8. Run the project.

No comments:

Post a Comment