Hi I have a grid that I need to send values to the RetrieveDataMethod. I am using 2 drop down menus like filters but because the menus don't relate directly to the columns in my grid I can't use the filter option.

I really like the Idea of using the client side binding because it is clean and avoids me needing to write any jquery.

The code for my grid is

public static MVCGridBuilder<Batch> BuildGrid(GridDefaults gridDefaults, ColumnDefaults columnDefaults)
    {
        return new MVCGridBuilder<Batch>(gridDefaults, columnDefaults)
            .AddColumns(cols =>
            {
                cols.Add("BatchId")
                    .WithHeaderText("Batch Id")
                    .WithValueExpression(p => p.BatchId.Trim());
                cols.Add("PartNumber").WithHeaderText("Part Number")
                    .WithValueExpression(p => p.PartNumber.Trim());
                cols.Add("AdjustedQty")
                    .WithHtmlEncoding(false)
                    .WithHeaderText("Adjusted Qty")
                    .WithValueExpression((p, c) => c.UrlHelper.Action("AdjustBatchQuantity", "Batch"))
                    .WithValueTemplate("<a href='#' id='AdjustedQty' style='display: inline;' data-type='text' data-pk='{Model.BatchId}' data-url='AdjustBatchQuantity' data-title='Adjust Quantity' class='editable editable-click' >{Model.AdjustedQty}</a>");
                cols.Add("UpdateDate")
                    .WithHeaderText("Update Date")
                    .WithValueExpression(p => p.UpdateDate.ToShortDateString());
            })
            .WithAdditionalQueryOptionNames("FamilyId", "ColorId")
            .WithAdditionalSetting("RenderLoadingDiv", true)
            .WithDefaultSortColumn("UpdateDate")
            .WithDefaultSortDirection(SortDirection.Dsc)
            .WithFiltering(true)
            .WithRetrieveDataMethod(context =>
            {
                var options = context.QueryOptions;

                var batchRepo = DependencyResolver.Current.GetService<IBatchRepository>();
                var productRepo = DependencyResolver.Current.GetService<IProductRepository>();

                int family = 0, bottleQty = 0, colorId = 0, count;

                if (options.AdditionalQueryOptions.ContainsKey("FamilyId"))
                {
                    var temp = options.GetAdditionalQueryOptionString("FamilyId");

                    if (temp != null)
                    {
                        family = int.Parse(options.GetAdditionalQueryOptionString("FamilyId"));
                    }
                }

                if (options.AdditionalQueryOptions.ContainsKey("ColorId"))
                {
                    var color = options.GetAdditionalQueryOptionString("ColorId");

                    if (color != null)
                    {
                        var temp = color.Split('_');

                        if (temp[0] != string.Empty)
                        {
                            colorId = Convert.ToInt32(temp[0]);
                        }

                        if (temp.Length > 1)
                        {
                            bottleQty = Convert.ToInt32(temp[1]);
                        }
                    }
                }

                var partNumbers = productRepo.GetPartNumbersByFamilyColorQuantity(family, colorId, bottleQty);
                var items = batchRepo.GetBatcheHistoryRecordsForGrid(options.GetLimitOffset(), options.GetLimitRowcount(), partNumbers, out count);

                if (!string.IsNullOrWhiteSpace(options.SortColumnName))
                {
                    switch (options.SortColumnName.ToLower().Replace(" ", string.Empty))
                    {
                        case "batchid":
                            items = items.OrderBy(p => p.BatchId).ToList();
                            break;
                        case "partnumber":
                            items = items.OrderBy(p => p.PartNumber).ToList();
                            break;
                        case "adjustedqty":
                            items = items.OrderBy(p => p.AdjustedQty).ToList();
                            break;
                        case "updatedate":
                            items = items.OrderBy(p => p.UpdateDate).ToList();
                            break;
                    }
                }

                if (options.SortDirection.ToString().ToLower() == "dsc")
                {
                    items.Reverse();
                }

                return new QueryResult<Batch>() { Items = items, TotalRecords = count };
            });
    }

And the relevant parts of my HTML are

@Scripts.Render("~/Areas/MfgTransactions_MVC/Scripts/CreateBatchDropDownFilters.js")

@Scripts.Render("~/bundles/BootstrapGridScripts")
@Styles.Render("~/bundles/Bootstrap3/BootstrapGridCss");

<script>
    $(document).ready(function () {
        $('#AdjustedQty').editable();
        $.fn.editable.defaults.mode = 'inline';
        $.fn.editable.defaults.ajaxOptions = {
            ajaxOptions: {
                type: 'POST',
                dataType: 'json'
            }
        };
    });
</script>

....

<div class="form-group ">
    @Html.LabelFor(x => x.FamilyId)
    @Html.DropDownListFor(x => x.FamilyId, Model.FamilyListItems, "Select Family", new { @class = "form-control", data_mvcgrid_type = "additionalQueryOption", data_mvcgrid_option = "FamilyId", data_mvcgrid_apply_additional = "change" })
 </div>

 <div class="form-group ">
     @Html.LabelFor(x => x.Color)
     @Html.DropDownListFor(x => x.Color, Model.ColorListItems, "Select Colour", new { @class = "form-control", data_mvcgrid_type = "additionalQueryOption", data_mvcgrid_option = "ColorId", data_mvcgrid_apply_additional = "change" })
 </div>

The problem seems to be that the additionalQueryOption event isn't been triggered. When I debug my code I can see that the line var options = context.QueryOptions; has the AdditionalQueryOptions but there are no values for them.

Is there a step that I have missed for setting this up ?

Related posts

Recent Viewed