Part 03 – Sorting, Paging, and Grouping Entity Framework NTPCUG Tom Perkins.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

PubMed Overview From the main HINARI webpage, we can access PubMed by clicking on Search HINARI journal articles through PubMed (Medline). Note: If you.
Database Basics. What is Access? Database management system Computer-based equivalent of a manual database Makes it easy to organize and update information.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Introduction to MVC Action Methods, Edit View, and a Search Feature NTPCUG Dr. Tom Perkins.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
An End-User Perspective On Using NatQuery Building a Dynamic Variable T
Using Visual Basic 6.0 to Create Web-Based Database Applications
Cognos 8.4 Upgrade Business Intelligence. Why Cognos 8.4 Increased Performance on Database due to optimized SQL and more filters passed in native SQL.
Introduction to Entity Framework Part 1 Tom Perkins NTPCUG.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Part 05 – Code First Migrations and Azure Deployment Entity Framework and MVC Series Tom Perkins NTPCUG.
Figure 1. Hit analysis in 2002 of database-driven web applications Hits by Category in 2002 N = 73,873 Results Reporting 27% GME 26% Research 20% Bed Availability.
What is so good about Archie and RevMan 5
Database Updates Made Easy In WebFocus Using SQL And HTML Painter Sept 2011 Lender Processing Services 1.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
A First Program Using C#
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Introduction to MVC Adding Model Classes NTPCUG Tom Perkins, Ph.D.
Databases and LINQ Visual Basic 2010 How to Program 1.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Chapter 10: Working with Large Data Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Part 06 – A More Complex Data Model Entity Framework and MVC NTPCUG Tom Perkins.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Using Arrays and File Handling
E-Commerce: Introduction to Web Development 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics What is a Web.
1 Data List Spreadsheets or simple databases - a different use of Spreadsheets Bent Thomsen.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Microsoft Access Lesson 1 Lexington Technology Center February 11, 2003 Bob Herring On the Web at
Chapter 3: Using GUI Objects and the Visual Studio IDE.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Introduction to Entity Framework Part 2 CRUD Scaffolding Tom Perkins NTPCUG.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
Page 1 Non-Payroll Cost Transfer Enhancements Last update January 24, 2008 What are the some of the new enhancements of the Non-Payroll Cost Transfer?
CSC 2720 Building Web Applications Server-side Scripting with PHP.
SESSION 3.1 This section covers using the query window in design view to create a query and sorting & filtering data while in a datasheet view. Microsoft.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Using Microsoft Visual Studio 2005 Original by Suma Rao Revised by John G. McMahon ( 9/6/2008 )
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Introduction to MVC Controllers NTPCUG Tom Perkins, Ph.D.
® IBM Software Group © 2006 IBM Corporation Using JSF Mini-Calendar Controls This section describes how to use a JSF Mini-Calendar Control with EGL It.
Creating Your First Web Page – Topic: Creating a wiki, blog, image blog and podcast Click Arrow - Next Slide 1.
Select (drop-down list) Inputs. Insert/Form/List Menu.
LANDESK SOFTWARE CONFIDENTIAL Tips and Tricks with Filters Jenny Lardh.
®® Microsoft Windows 7 for Power Users Tutorial 3 Managing Folders and Files.
® Microsoft Access 2010 Tutorial 5 Creating Advanced Queries and Enhancing Table Design.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Web QT Today Runs against the Online Transaction Processing (OLTP) Production Database Uses J2EE Architecture Designed to provide operational support.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
Welcome to State of Michigan Managerial and SupervisoryAdvance Approval & Modification Approval & Modification Tutorial Brought to you by the Office of.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
CIS 338: VB.NET Printing Dr. Ralph D. Westfall May, 2011.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
CHAPTER 7 LESSON B Creating Database Reports. Lesson B Objectives  Describe the components of a report  Modify report components  Modify the format.
Mail Merge Introduction to Word Processing ITSW 1401 Instructor: Glenda H. Easter Introduction to Word Processing ITSW 1401 Instructor: Glenda H. Easter.
Jim Fawcett CSE686 – Internet Programming Spring 2014
Visual Basic 2010 How to Program
Jim Fawcett CSE686 – Internet Programming Spring 2012
02 | Developing ASP.NET MVC 4 Models
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Tutorial 7 – Integrating Access With the Web and With Other Programs
Guidelines for Microsoft® Office 2013
IS 135 Business Programming
Presentation transcript:

Part 03 – Sorting, Paging, and Grouping Entity Framework NTPCUG Tom Perkins

Previously … Contoso University solution created Database Student Model, Views, and Controller CRUD Scaffolding

Session Objectives 1.Sorting – display student data in different orders by clicking on the column headings. 2.Filtering – Search student names that contain a string of characters 3.Paging – Show pages of a long list of students 4.Grouping – Show student statistics derived through grouping

Target “Students” Page

COLUMN HEADER SORT LINKS Objective 1

Column Headings Headings: Last Name and Enrollment Date Click on heading to sort list Repeated clicking – toggles list in ascending versus descending order Headings are hyperlinks to a controller action

Student Activity Open Visual Studio Open the Contoso University solution In the Solution Explorer – In the Controllers section, Click on StudentController.cs Locate the Index method Replace the code in the Index method by cutting and pasting the code in Part 3 Timesaver Modification 1 // GET: /Student/ public ActionResult Index() { return View(db.Students.ToList()); }

public ActionResult Index(string sortOrder) { ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; var students = from s in db.Students select s; switch (sortOrder) { case "name_desc": students = students.OrderByDescending(s => s.LastName); break; case "Date": students = students.OrderBy(s => s.EnrollmentDate); break; case "date_desc": students = students.OrderByDescending(s => s.EnrollmentDate); break; default: students = students.OrderBy(s => s.LastName); break; } return View(students.ToList()); }

The New Index Method Refer to Modification 1: Method receives “sortorder” string as input sortorder values expected: – Name – name_desc ( name descending) – Date – date_desc ( date descending) Default – Last Name, ascending

Processing Logic Refer to Modification 1: First time through: – sortorder is null – List is displayed in ascending Name order Entered as a result of user clicking column heading: – sortorder value will be established in the query string created by the hyperlink – “Name”, “name_desc”, “Date”, “date_desc”)

Controller/View Communication The View simply displays the List of Students sent to it by the Controller. The Controller establishes the order for the Student List. If the user wishes to see a different order, s/he clicks on the column header. This will generate a subsequent query back to the controller, identifying which column has been selected and the requested ascending/descending order.

The ViewBag Variables The ViewBag can be used to share information between the Controller and the View Two ViewBag variables used here: – ViewBag.NameSortParm – ViewBag.DateSortParm Values are set in the Controller Values are used in the View to configure the column header hyperlinks The column header hyperlinks – Identify what order the display is to be changed to – “toggle” whether the display is to be in ascending or descending order

MVC Principle The bulk of the processing is done by the Controller, on the data in the Model. The View simply displays the data in the Model. Controls on the View may be configured from data in the ViewBag to request actions elsewhere in the application – here, in another display from the originating controller.

ViewBag Variables Refer to Modification 1 Ternary statements – (composed of three parts) If sortorder parm is null or empty (implies this is the default first time through or a name-ascending display was requested – Set the NameSortParm to “name-descending” (toggle the LastName heading to generate a descending request if clicked) – ELSE – Set the NameSortPart to “ “ (empty string – configure the Last Name heading to generate an ascending request if clicked) ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

The sortOrder parameter Describes the order in which the student list will be displayed Sets up the hyperlinks in the column headers for a subsequent user click Current sort orderLast Name HyperlinkDate Hyperlink Last Name ascendingdescendingascending Last Name descendingascending Date ascendingascendingdescending Date descendingascending

Student Activity Apply the highlighted code in Modification 2 to Views\Student\index.cshtml

Run the page Click the Date column header and verify that the list is in Date order Click the LastName column header and verify that the list is in Last Name order

ADD A SEARCH TEXT BOX – LAST NAME, FIRST NAME SEARCH Objective 2

Student Activity Apply Modification 4 to Views\Student\index.cshtml

Student Activity Apply the entire Modification 5 to replace the index method in Controllers\StudentController.cs (changes are hightlighted)

Run the Student Page Search for ‘an’ in the first or last name

CREATE A PAGING FEATURE Objective 3

Paging Feature Approach Install a NuGet Package (PagedList.MVC) Change the Index method Add paging links to the Index view.

PagedList.Mvc – NuGet Package Installs a collection type and extension methods – For both IQueryable collections (Outside of memory – LINQ or SQL) IEnumerable collections (In memory) Extension methods  a single page of data in a PagedList collection PagedList collection provides properties and methods that facilitate paging A paging helper is installed that can display the paging buttons

STUDENT ACTIVITY Tools Menu | Library Package Manager | Package Manager Console In Console, make sure: – Package Source = nuget.org – Default Project = ContosoUniversity Then type Install-Package PagedList.Mvc

Analysis of modified StudentController Index method New parameters: – page – current sort order – current filter All parameters null if – First time through – User hasn’t clicked sorting or paging link If paging link has been clicked, page parameter contains the page number to display Current sort order is passed to View through the ViewBag – The current sort order is required to keep the same sort order whilst paging. public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page) ViewBag.CurrentSort = sortOrder;

Code analysis, continued … currentFilter – provides the current filter string. If search string is changed during paging – Page must be set to 1 (different data will be displayed) When new search value is entered into text box and submit pressed, searchString will not be null. if (searchString != null) { page = 1; } else { searchString = currentFilter; }

Bottom of code – analysis continued … ToPagedList extension method passes a single page of students to the View. Page is in a collection type that supports paging. (page??1) – ?? null coalescing operator – Returns page if not null, else returns 1 int pageSize = 3; int pageNumber = (page ?? 1); return View(students.ToPagedList(pageNumber, pageSize));

STUDENT ACTIVITY Modify Student Index View for Paging Replace all the code in Views\Student\Index.cshtml with the code in Modification 7. – Note: cut and paste all the code; it extends over two pages

Code Analysis Please refer to code in Modification statement – View now gets a PagedList object instead of a List object using – access MVC helper for paging buttons BeginForm now specifies FormMethod.Get – Default BeginForm - parameters passed in message body (POST) – Get recommended when action does not result in an update – W3C – Get passes parameters in query string – user can bookmark the URL

Code Analysis, continued Initializing the text box with the current search string allows the user to see the search string when a new page is displayed Header links are now passed the current filter so that the user can sort within filter results Current page and total number of pages are displayed Page 0 of 0 is displayed Paging buttons are displayed by PageListPager helper

STUDENT ACTIVITY Run the page Verify paging works Verify filtering within paging works

STUDENT STATISTICS PAGE Objective 4

Student Statistics Page Displays how many students have enrolled for each enrollment date Involves grouping and simple group calculations Approach: – New model class for View – Modify About method in HomeController – Modify the About View

STUDENT ACTIVITY Add a new folder to the project (ViewModels) Add a new class to this folder (EnrollmentDateGroup.cs) Replace the template code with the code in Modification 8. using System; using System.ComponentModel.DataAnnotations; namespace ContosoUniversity.ViewModels { public class EnrollmentDateGroup { [DataType(DataType.Date)] public DateTime? EnrollmentDate { get; set; } public int StudentCount { get; set; } }

STUDENT ACTIVITY In HomeController.cs: Add the following using statements at the top of the file. using ContosoUniversity.DAL; using ContosoUniversity.ViewModels; Add a class variable for the database context immediately after the curly brace public class HomeController : Controller { private SchoolContext db = new SchoolContext(); Replace the About method in HomeController.cs with the code in Modification 9. Add a Dispose method to HomeController.cs as shown in Modification 10.

STUDENT ACTIVITY Replace the code in module Views\Home\About.cshtml with the code in Modification 11. Run the app and view the About screen.

FINIS