在 .Net MVCGrid 中,我无法使用客户端绑定传递其他查询选项
In .Net MVCGrid I'm unable to pass additional query options using client side bindings
您好,我有一个网格,我需要将值发送到 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 但它们没有值。
设置时我是否遗漏了一个步骤?
不好意思,我删了
@Scripts.Render("~/MVCGridHandler.axd/script.js")
并将它放在我的包配置中,但它似乎在那里不起作用。
您好,我有一个网格,我需要将值发送到 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 但它们没有值。
设置时我是否遗漏了一个步骤?
不好意思,我删了
@Scripts.Render("~/MVCGridHandler.axd/script.js")
并将它放在我的包配置中,但它似乎在那里不起作用。