Back to Devexpress

Saving Queries

aspnetmvc-120083-components-query-builder-saving-queries.md

latest4.8 KB
Original Source

Saving Queries

  • Dec 17, 2020
  • 2 minutes to read

The Query Builder sends dedicated requests to the server using callbacks when an end user clicks the Save toolbar button or the Save client-side event is called. You should implement a controller action to process these callbacks for saving queries.

In the QueryBuilderExtension‘s declaration, use the QueryBuilderSettings.SaveCallbackRouteValues property to specify the names of the controller and action that handle these callbacks.

cshtml
@Html.DevExpress().QueryBuilder(settings => {
    settings.Name = "QueryBuilder";
    settings.RouteValues = new { Controller = "QueryBuilder", Action = "Invoke" };
    settings.SaveCallbackRouteValues = new { Controller = "QueryBuilder", Action = "Save" };
}).Bind("NorthwindConnection").GetHtml()

Add the corresponding Save action to the Query Builder controller and call the QueryBuilderExtension.GetSaveCallbackResult method to obtain the callback result. The returned object provides the following properties that represent the resulting query:

Check the QueryBuilderSaveCallbackResult.ErrorMessage property value to get an error text if an exception occurred during query validation.

csharp
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using DevExpress.DataAccess.Sql;

public class QueryBuilderController : DevExpress.Web.Mvc.Controllers.QueryBuilderApiController {
    public override ActionResult Invoke() {
        return base.Invoke();
    }

    public ActionResult Save() {
        var result = QueryBuilderExtension.GetSaveCallbackResult("QueryBuilder");
        if (!string.IsNullOrEmpty(result.ErrorMessage)) {
            return Json(new { queryValidationError = result.ErrorMessage });
        }
        SelectQuery query = result.ResultQuery;
        string selectStatement = result.SelectStatement;
        // ...
    }
}
vb
Imports System.Web.Mvc
Imports DevExpress.Web.Mvc
Imports DevExpress.DataAccess.Sql

Public Class QueryBuilderController
    Inherits DevExpress.Web.Mvc.Controllers.QueryBuilderApiController

    Public Overrides Function Invoke() As ActionResult
        Return MyBase.Invoke()
    End Function

    Public Function Save() As ActionResult
        Dim result = QueryBuilderExtension.GetSaveCallbackResult("QueryBuilder")
        If Not String.IsNullOrEmpty(result.ErrorMessage) Then
            Return Json(New With {Key.queryValidationError = result.ErrorMessage})
        End If

        Dim query As SelectQuery = result.ResultQuery
        Dim selectStatement As String = result.SelectStatement
    End Function
End Class

You can also use the MVCxQueryBuilderClientSideEvents.SaveCommandExecuted client-side event to process the saving query result on the client.

cshtml
<script>
    function onSaveCommandExecuted(s, e) {
        var result = JSON.parse(e.Result);
        if (result.queryValidationError) {
            alert(result.queryValidationError);
        }
    }
</script>

@Html.DevExpress().QueryBuilder(settings => {
    ...
    settings.ClientSideEvents.SaveCommandExecuted = "onSaveCommandExecuted";
}).Bind("NorthwindConnection").GetHtml()

See Also

Lesson 1 - Add the Query Builder to a Project

Lesson 2 - Use the Resulting Query