Skip to main content
All docs
V23.2

Create Editors for Custom Parameter Types in an ASP.NET MVC Application

  • 8 minutes to read

This topic explains how to implement a custom parameter type, add custom type parameters to a report, and create a custom editor for the Document Viewer’s Parameters Panel. The custom parameter type defines an email address.

Create a Custom Parameter Type and a Converter

Define a CustomParameterType class with the Value property. Implement a CustomParameterTypeConverter converter to display a parameter value in a document.

using System;
using System.ComponentModel;
using System.Globalization;

namespace CustomParameterEditorAspNetMvcExample
{
    [TypeConverter(typeof(CustomParameterTypeConverter))]
    public class CustomParameterType
    {
        public string Value { get; set; }
        public override string ToString()
        {
            return Value;
        }
    }

    public class CustomParameterTypeConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context,
            CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return ((CustomParameterType)value).Value;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context,
            Type destinationType)
        {
            return destinationType == typeof(string) ||
                base.CanConvertTo(context, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context,
            CultureInfo culture, object value)
        {
            var valueString = value as string;
            if (valueString != null)
            {
                return new CustomParameterType { Value = valueString };
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}

Implement a Custom Parameter Serializer

A serializer is necessary to pass data from the client to the controller on the server and store the parameter value in report definition files.

using DevExpress.XtraReports.Native;

namespace CustomParameterEditorAspNetMvcExample
{
    public class CustomDataSerializer : IDataSerializer
    {
        public const string Name = "myCustomDataSerializer";

        public bool CanDeserialize(string value, string typeName, object extensionProvider)
        {
            return typeName == typeof(CustomParameterType).FullName;
        }

        public bool CanSerialize(object data, object extensionProvider)
        {
            return data is CustomParameterType;
        }

        public object Deserialize(string value, string typeName, object extensionProvider)
        {
            if (typeName == typeof(CustomParameterType).FullName)
            {
                return new CustomParameterType { Value = value };
            }
            return null;
        }

        public string Serialize(object data, object extensionProvider)
        {
            var parameter = data as CustomParameterType;
            return parameter != null ? parameter.Value : null;
        }
    }
}

Register the Custom Parameter Serializer at Startup

Add the following code to the Global.asax file to register the CustomDataSerializer service in the application:

public class MvcApplication : System.Web.HttpApplication {
    protected void Application_Start() {
    // ...
        DevExpress.XtraReports.Native.SerializationService.RegisterSerializer(
            CustomDataSerializer.Name, new CustomDataSerializer());
            // ...
    }
    // ...
}

Add the Custom Parameter Type to the Report Designer

Handle the Report Designer’s Init event to add a custom parameter type (Custom Type) and specify the custom-parameter-editor HTML template as the parameter’s editor. The editor template is based on the dxTextBox editor.

@model DevExpress.XtraReports.UI.XtraReport

<script type="text/html" id="custom-parameter-editor">
    <div data-bind="dxTextBox: { value: value }, 
         dxValidator: { validationRules: [{ type: 'email', message: 'Email is not valid.' }]}">
    </div>
</script>
<script type="text/javascript">
    function reportDesignerInit(sender, e) {
        var editor = { header: "custom-parameter-editor" };
        sender.AddParameterType({
            displayValue: "Custom Type",
            specifics: "custom",
            value: "@typeof(CustomParameterEditorAspNetMvcExample.CustomParameterType).FullName",
            valueConverter: function(valueObj) { return valueObj; }
        }, editor);
    }
    <!-- ... -->
    }
</script>

@Html.DevExpress().ReportDesigner(settings =>
{
    settings.Name = "ReportDesigner1";
    settings.ClientSideEvents.Init = "reportDesignerInit";
    <!-- ... -->
}).Bind(Model).GetHtml()

Run the project and open the Report Designer. Save the empty report to a file (Template.repx). This file contains a serialized extension that references the custom type serializer. You can use the Template.repx file as a new report template.

Use Template for New Reports

All reports created with the New command in the Report Designer should be based on the custom template with the custom type serializer. Otherwise, a custom report parameter causes an error when the report is opened.

Modify the New command so that it opens a Template report:

@model DevExpress.XtraReports.UI.XtraReport

<script type="text/html" id="custom-parameter-editor">
    <div data-bind="dxTextBox: { value: value }, 
         dxValidator: { validationRules: [{ type: 'email', message: 'Email is not valid.' }]}">
    </div>
</script>
<script type="text/javascript">
    function reportDesignerInit(sender, e) {
        var editor = { header: "custom-parameter-editor" };
        sender.AddParameterType({
            displayValue: "Custom Type",
            specifics: "custom",
            value: "@typeof(CustomParameterEditorAspNetMvcExample.CustomParameterType).FullName",
            valueConverter: function(valueObj) { return valueObj; }
        }, editor);
    }
    function onCustomizeMenuActions(s, e) {
        var newReportAction = e.GetById(DevExpress.Reporting.Designer.Actions.ActionId.NewReport);
        if (newReportAction) {
            newReportAction.clickAction = function () {
                s.OpenReport("TemplateReport");
            }
        }
    }
</script>

@Html.DevExpress().ReportDesigner(settings =>
{
    settings.Name = "ReportDesigner1";
    settings.ClientSideEvents.Init = "reportDesignerInit";
    settings.ClientSideEvents.CustomizeMenuActions = "onCustomizeMenuActions";
}).Bind(Model).GetHtml()

Register a Custom Serializer When Reports are Saved

The application should register a custom serializer for all reports when they are saved to avoid an error if the user adds a custom parameter to that report.

The application uses a custom report storage to save a report and open it from a file. Modify the storage’s SaveData method to register the custom serializer for all saved reports:

public override void SetData(XtraReport report, string url) {
    // Stores the specified report to a Report Storage using the specified URL. 
    // This method is called only after the IsValidUrl and CanSetData methods are called.
    var resolvedUrl = Path.GetFullPath(Path.Combine(reportDirectory, url + FileExtension));
    if (!resolvedUrl.StartsWith(reportDirectory + Path.DirectorySeparatorChar)) {
        throw new FaultException("Invalid report name.");
    }

    report.SaveLayoutToXml(resolvedUrl);
}

Run the Application

The Document Viewer opens the CustomParameterReport, which contains custom parameters. The Parameters panel displays custom editors for custom parameter types:

Parameters Panel with Custom Parameter Editors

This example is based on the project created from the ASP.NET MVC Reporting template in our Web App Template Gallery. For more information on the Template Gallery, review the following topic: Create an ASP.NET MVC Application with a Report Designer.

View Example: Custom Report Parameter Types in Web Reporting Controls (ASP.NET MVC)