Click here to Skip to main content
11,571,102 members (65,620 online)
Click here to Skip to main content

Tagged as

An MVC multi-format view reporting application using JSON and LINQ

, 9 May 2013 CPOL 5.4K 479 13
Rate this:
Please Sign up or sign in to vote.
This tip contains a solution for rendering reports which can be viewed (or saved) with different extensions.

Sample

Introduction

This tip contains a solution for rendering reports which can be viewed (or saved) with different extensions. The solution also illustrates how you can use LINQ grouping using the GroupBy standard query operator where a subtotal is needed in a report. The application uses Flexigrid for jQuery - v1.1 and JSON with LINQ to handle the data access in the grid.

Background

The ASP.NET MVC Framework's Model View Controller pattern can be used efficiently to display reports in multiple formats. In this sample application, we are considering a hypothetical branded product which is sold by stores including Macy’s, JCPenny, Target, Dillard, and WalMart. There are also various promotional offers like Online, Catalog, and Outlet. Suppose we want to get the total sale price of the product sold in a particular store, for example, say Macy’s. We are using LINQ grouping to get the result.

Using the Code

The application at first creates a set of collections of transaction items, selected according to the specific store. This is followed by an into clause. The into clause can be used with various operations, including selects, joins, and groups. The clause causes the creation of a temporary variable for use within the query. The variable stores the results of a query and can itself be queried further. To run the application, open the Visual Studio 2012 project and start. In our sample application, we are using a repository (class) that performs the LINQ query to retrieve data from the input XML (note that we are using an XML file to populate the data in the grid). The MVC controller instantiates and calls the repository method to get the data. Once the data is available, the application uses HttpContext to store these information to make it accessible throughout all layers. Moreover, the controller assigns the Transaction (i.e., trans) to the session called data1. Now the session object data1 can be used by the GUI to render data in a different format.

var grouped = from acct in trantotals
              group acct by acct.Loc+acct.ProgramId into g
              select new TransactionTotals()
              {
                  Loc = g.First().Loc,
                  ProgramId = g.First().ProgramId,
                  Amt = g.Sum(a => a.Amt),
                  Discount = g.Sum(a=>a.Discount),                  
                  Count = g.Sum(a=>a.Count)

              };

HttpContext.Session.Add("data1", trans);
IList<TransactionTotals> trans = 
  (IList<TransactionTotals>)HttpContext.Session["data1"];

Here the controller assigns the transaction (i.e., trans) to the session called data1.

Points of Interest

This application works for small or short-lived applications but can also be extended for large applications.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ankurkumarbora
Software Developer (Senior)
United States United States
I am passionate on coding and application development specially in the .NET C# windows and web platform.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 9 May 2013
Article Copyright 2013 by Ankurkumarbora
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid