1

您好我有一个网格,我需要将值发送到 RetrieveDataMethod。我正在使用 2 个下拉菜单,例如过滤器,但由于菜单与网格中的列没有直接关系,因此我无法使用过滤器选项。

我真的很喜欢使用客户端绑定的想法,因为它很干净并且避免了我需要编写任何 jquery。

我的网格的代码是

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 };
            });
    }

我的 HTML 的相关部分是

@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>

问题似乎是未触发 additionalQueryOption 事件。当我调试我的代码时,我可以看到该行var options = context.QueryOptions;具有 AdditionalQueryOptions 但它们没有值。

有没有我错过了设置这个步骤?

4

1 回答 1

0

我的坏,我已经删除了

@Scripts.Render("~/MVCGridHandler.axd/script.js")

并将它放在我的捆绑配置中,但它似乎在那里不起作用。

于 2016-02-08T14:16:12.330 回答