Sometimes creating a report can turn into to be a real headache. For example, when you need to manage the content of the report based on the conditions in the program. It is easier to do this in the program code than to transfer parameters to the report and develop the report script logic.
Today we will look at how to create a report from the application code, not a simple desktop application, but the ASP.NET Core MVC web application. We have already looked at how to create a report in the code of a WinForms application. However, web applications are much more popular now and many FastReport users would like to use all the features of this report generator in them.
Let's take a look at how to create a report template and run it without a report designer. First, we need an ASP.NET Core MVC application project. To work with the FastReport.NET report generator, you need to install its libraries using the NuGet package manager.
Since the library packages are located on the local disk (in the directory of the installed program in the Nuget folder), we need to add a local package source. To do this, click on the gear icon in the upper right corner of the package manager and add a new source that will link to the local folder with your packages (with the .nupkg extension):
Now you can select the added package source from the drop-down list and install the packages.
To use the added libraries in the program code, you must first connect them in the Startup.cs file. Let's add a string in the configuration method:
Now you can go to programming. Our MVC project has:
- Controllers that contain the logic of the server-side of the application.
- Views display information for the client.
- Models are responsible for working with data (reception, storing, transformation and recording).
Our simple example will only require a controller and a view. Let's change the Index method in the controller. Our application, which is automatically generated from a template, already has a ready controller - HomeController:
It is clear from the comments to the code that we create report objects manually and build their hierarchy. Now we have created a tabular report with pictures in cells. The desired picture is selected depending on the price value. This is just a typical example when you may need to create a report from the program code.
Now let's change the Index.cshtml view. Let's leave just one line:
That is all. You can run the application and see our report:
Let’s summarize. In fact, creating a report from the code of a web application is no different from a regular desktop application. On the one hand, it requires a deep knowledge of the structure of the report and its objects, which means that a qualified specialist should create it. On the other hand, sometimes this is the only way to create a report with complex logic. So, don’t take this method as the main one, but it will help you in the hour of need.