Custom Error Message

Try sorting by first name to see the error message.

Id First Name Last Name
370PatrickAdams
493KarenAdams
570NancyAdams
599EricAdams
635ChristineAdams
663BonnieAdams
778RonaldAdams
820SeanAdams
897JudithAdams
857MichaelAlexander
652HowardAlexander
339JerryAlexander
55SusanAlexander
190AmyAlexander
250NicholasAllen
472DeniseAllen
171JessicaAlvarez
127JanetAlvarez
661MildredAlvarez
855MarieAlvarez
Showing 1 to 20 of 1000 entries

Code

Inside MVCGridConfig.cs
MVCGridDefinitionTable.Add("CustomErrorMessage", new MVCGridBuilder<Person>(colDefauls)
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        cols.Add("Id").WithSorting(false)
            .WithValueExpression(p => p.Id.ToString());
        cols.Add("FirstName").WithHeaderText("First Name")
            .WithValueExpression(p => p.FirstName);
        cols.Add("LastName").WithHeaderText("Last Name")
            .WithValueExpression(p => p.LastName);
    })
    .WithErrorMessageHtml(@"<div class=""alert alert-danger"" role=""alert"">OH NO!!!</div>")
    .WithSorting(true, "LastName")
    .WithPaging(true)
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        var result = new QueryResult<Person>();
        using (var db = new SampleDatabaseEntities())
        {
            var query = db.People.AsQueryable();
            result.TotalRecords = query.Count();
            if (!String.IsNullOrWhiteSpace(options.SortColumnName))
            {
                switch (options.SortColumnName.ToLower())
                {
                    case "firstname":
                        throw new Exception("Test exception");
                    case "lastname":
                        query = query.OrderBy(p => p.LastName, options.SortDirection);
                        break;
                }
            }
            if (options.GetLimitOffset().HasValue)
            {
                query = query.Skip(options.GetLimitOffset().Value).Take(options.GetLimitRowcount().Value);
            }
            result.Items = query.ToList();
        }
        return result;
    })
);
View
@Html.MVCGrid("ErrorMessage")