Friday, 22 February 2013

ASP.NET - Entity Framework and Model Binding FormView ListView simple example

Watch this example online:



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

 2. First  create new project in Visual Studio 2012.  Add ADO.NET Entity Data Model to it and select StyleMaster table (all details in video).
3. Add new web page.
4. Add FormView to the page
        <asp:FormView ID="FormView1" runat="server" ItemType="WebApplication16.StyleMaster" SelectMethod="GetStyle">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <%#: Item.StyleDescription %>
                            <br />
                            <%#: Item.StyleName %>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:FormView>
5. Ensure that ItemType is set to WebApplication16.StyleMaster (you may have to rebuild project to get intellisense, also your project most likely will have different name, not WebApplication16)
6. Ensure that SelectMethod is set to GetStyle.
7. Implement GetStyle method:
        public List<StyleMaster> GetStyle()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                IQueryable<StyleMaster> result = db.StyleMasters;
                result = result.Where(s => s.StyleId == 3);
                return result.ToList();
            }
        }
8. Run the project.  It will populate form view with info about style id 3.  to make this example more realistic, I will add another page that will display groups.  Each group will have 1 (or more but I will ignore it) style.  Once user click the group - first associated style with that group will be populated in form view.
9. My GroupMaster table looks like this:

10. Update Entity Model (watch video how to do this)
11. Add new web page, name it First Page, set as start page, add list view
        <asp:ListView ID="ListView1" runat="server" ItemType="WebApplication16.GroupMaster" SelectMethod="GetGroups">
            <ItemTemplate>
                <a href="DisplayStyleInFormView.aspx?Id=<%#: Item.GroupId %>"><%#: Item.GroupName %></a>
            </ItemTemplate>
        </asp:ListView>
12. Generate GetGroups function
        public List<GroupMaster> GetGroups()
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                var result = db.GroupMasters.ToList();
                return result;
            }
        }
13. Modify GetStyle function
        public List<StyleMaster> GetStyle([QueryString("Id")] int? groupId)
        {
            using (CompanyTestDatabaseEntities db = new CompanyTestDatabaseEntities())
            {
                IQueryable<StyleMaster> result = db.StyleMasters;
                if (groupId.HasValue && groupId > 0)
                {
                    result = result.Where(s => s.GroupId == groupId);
                }
                else
                {
                    result = result.Where(s => s.StyleId == 3);//default to group id 3
                }
               
                return result.ToList();
            }
        }
14 run.

No comments:

Post a Comment