Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
9 May 2013 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here