Saving Queries
- 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.
@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:
- QueryBuilderSaveCallbackResult.ResultQuery - Provides a SelectQuery object storing an SQL query’s high-level model that you can reopen in the Query Builder for further editing (using the appropriate QueryBuilderExtension.Bind method overload).
- QueryBuilderSaveCallbackResult.SelectStatement - Provides a string specifying the final ‘SELECT’ statement and optionally contains ‘WHERE’, ‘GROUP BY’ and ‘ORDER BY’ query clauses.
Check the QueryBuilderSaveCallbackResult.ErrorMessage property value to get an error text if an exception occurred during query validation.
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;
// ...
}
}
You can also use the MVCxQueryBuilderClientSideEvents.SaveCommandExecuted client-side event to process the saving query result on the client.
<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()