Custom Export

  • 2 minutes to read
TIP

Starting with version 17.2, we recommend using the client API and the built-in toolbar commands for ASPxCardView data exporting.

The ASP.NET MVC CardView extension allows exporting the MVCxCardView records to CSV, PDF, RTF, XLS and XLSX format using the methods listed in the Member Table: Custom Export topic.

Declare the master grid settings (a CardViewSettings type object) and an object the grid is bound to. We recommended declaring grid settings in the Controller code (or any other helper code) and passing them to the View code to avoid repeatedly declaring and initializing grid settings.

Example

The code sample below demonstrates how to implement the MVCxCardView data exporting.

Controller code:

using System.Linq;
using System.Web.Mvc;
using DevExpress.Web.Mvc;

namespace MyProject.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.GridSettings = GetGridSettings();
            return View();
        }
        // The Entity Framework data context
        MyProject.Models.northwndEntities db = new MyProject.Models.northwndEntities();

        // Handles CardView callbacks.
        [ValidateInput(false)]
        public ActionResult CardViewPartial()
        {
            ViewBag.GridSettings = GetGridSettings();
            var model = db.Products;
            return PartialView("_CardViewPartial", model.ToList());
        }

        // This action method sends a PDF document with the exported grid to response.
        public ActionResult ExportTo()
        {
            var model = db.Products;
            return CardViewExtension.ExportToPdf(GetGridSettings(), model.ToList());
        }

        // Returns the settings of the exported CardView.
        private CardViewSettings GetGridSettings() {
            var settings = new CardViewSettings();
            settings.Name = "CardView";
            settings.CallbackRouteValues = new { Controller = "Home", Action = "CardViewPartial" };

            // Export-specific settings 
            settings.SettingsExport.ExportSelectedCardsOnly = false;
            settings.SettingsExport.FileName = "Report.pdf";
            settings.SettingsExport.PaperKind = System.Drawing.Printing.PaperKind.A4;

            settings.KeyFieldName = "ProductID";
            settings.Columns.Add("ProductName");
            settings.Columns.Add("UnitPrice").PropertiesEdit.DisplayFormatString = "c";
            settings.Columns.Add("QuantityPerUnit");
            settings.Columns.Add("Discontinued", MVCxCardViewColumnType.CheckBox);

            return settings;
        }
    }
}

View code("Index"):

@Html.Action("CardViewPartial")

// When an end-user clicks this button, the button sends a callback to the "ExportTo" action and  
// the server sends the resulting export file to the response.
@Html.DevExpress().Button(settings =>
{
    settings.Name = "Button";
    settings.UseSubmitBehavior = false;
    settings.Text = "ExportTo PDF";
    settings.RouteValues = new { Controller = "Home", Action = "ExportTo" };
}).GetHtml()

Partial View code ("_CardViewPartial"):

@{
    var grid = Html.DevExpress().CardView(ViewBag.GridSettings);
}
@grid.Bind(Model).GetHtml()
See Also