Telerik MVC 网格 Ajax 参数?

发布于 2024-12-06 08:12:40 字数 2978 浏览 0 评论 0原文

我有点厌倦了尝试计算 Ajax 网格参数。我创建了几个网格,参数似乎有点随意,我尝试一件事,它对一个网格不起作用,然后对另一个网格起作用。

我的理解是,如果这些参数存在于 .DataKeys 集合中,您真的不必将这些参数放入 .DataBinding 中吗?当它有效和无效时,这似乎是任意的。

有人可以给我一个关于 Ajax 网格绑定和将参数传递到控制器的简短概述,以便我可以选择数据来填充它吗?为什么我需要定义参数,而有时它却像魔法一样工作?

最近,每一个小小的想法似乎都是与 Telerik MVC 控件的战斗,甚至是我以前做过 5-6 次的事情。

在这种情况下: LineItemID 是主键,JobID 是外键。我真的想将 JobID 传递给选择绑定。我想获取具有特定 JobID 的所有订单项。

视图:

@{  Grid<viaLanguage.Jams.Data.tblJobManagementLineItem> grid = Html.Telerik().Grid<viaLanguage.Jams.Data.tblJobManagementLineItem>()
        .Name("InvoiceLineItemsGrid")
        .DataKeys(keys =>
        {
            keys.Add(i => i.LineItemID);//.RouteKey("LineItemID");
            keys.Add(i => i.JobID);//.RouteKey("jobID");
        })
        .DataBinding(dataBinding => dataBinding.Ajax()
            .Select("_SelectInvoiceLineItems", "Job", new { jobID = "<#= JobID #>" })
            .Insert("_InsertJobInvoice", "Job")
            .Update("_UpdateJobInvoice", "Job")
            .Delete("_DeleteJobInvoice", "Job"))
        .ToolBar(commands => commands.Insert().HtmlAttributes(new { style = "margin-left:0" }))
        .Columns(columns =>
        {
            columns.Bound(l => l.JobID);
            columns.Bound(l => l.LineItemDescr).Width(180).HeaderTemplate("Task");
            columns.Bound(l => l.LanguagePairID).Width(100).HeaderTemplate("Language Pair").ClientTemplate("<#= LanguagePair #>");
            columns.Bound(l => l.SourceWordCount).Width(100).HeaderTemplate("Source Words");            
            columns.Bound(l => l.QtyToInvoice).Width(100).HeaderTemplate("Quantity to Invoice");
            columns.Bound(l => l.Rate).Width(50).Format("${0:0.000}").HeaderHtmlAttributes(new { style = "text-align: center;" }).HtmlAttributes(new { style = "text-align: center;" });
            columns.Bound(l => l.SubTotal).Width(100).Format("{0:c}");
            columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Image);
                commands.Delete().ButtonType(GridButtonType.Image);
            }).Width(80).HtmlAttributes(new { style = "white-space: nowrap;" });
        })
        .Resizable(resizing => resizing.Columns(true))
        .Sortable()
        .Selectable()
        .Pageable(paging => paging.PageSize(10))
        .Scrollable(c => c.Height("233px"))
        .Filterable();

    StringWriter sw = new StringWriter();
    grid.Render();
    grid.WriteInitializationScript(sw);
}           

@Html.Hidden("InvoiceLineItemsGrid_tGrid", sw.GetStringBuilder().ToString())

控制器:

[GridAction]
public ActionResult _SelectInvoiceLineItems(int jobID)
{
    logLogic.logger.Debug(logLogic.GetMethodName() + ": User '" + User.Identity.Name);
    return View(new GridModel(jobLogic.GetInvoiceLineItems(jobID, User.Identity.Name)));
}

提前感谢您的任何见解。 史蒂夫

I am kind of fed up with trying to figure Ajax Grid parameters. I have created several grids and the parameters just kind of seem to be willy-nilly, I try one thing, it doesn't work for one, then works for another.

My understanding is that you really don't have to put the parameters in the .DataBinding if those parameters exist in the .DataKeys collection? This seems to be arbitrary when it works and when it doesn't.

Can someone give me a short overview about Ajax grid binding and passing parameters to a controller so I can select data to populate it with? Why is it I need to define the parameters and other times it works like magic?

Lately, every little think seems to be a battle with the Telerik MVC controls, even stuff I have done 5-6 times before.

In this case:
LineItemID is the primary key and JobID is a foreign key. I really want to pass the JobID to the select binding. I want to grab all line items with a specific JobID.

View:

@{  Grid<viaLanguage.Jams.Data.tblJobManagementLineItem> grid = Html.Telerik().Grid<viaLanguage.Jams.Data.tblJobManagementLineItem>()
        .Name("InvoiceLineItemsGrid")
        .DataKeys(keys =>
        {
            keys.Add(i => i.LineItemID);//.RouteKey("LineItemID");
            keys.Add(i => i.JobID);//.RouteKey("jobID");
        })
        .DataBinding(dataBinding => dataBinding.Ajax()
            .Select("_SelectInvoiceLineItems", "Job", new { jobID = "<#= JobID #>" })
            .Insert("_InsertJobInvoice", "Job")
            .Update("_UpdateJobInvoice", "Job")
            .Delete("_DeleteJobInvoice", "Job"))
        .ToolBar(commands => commands.Insert().HtmlAttributes(new { style = "margin-left:0" }))
        .Columns(columns =>
        {
            columns.Bound(l => l.JobID);
            columns.Bound(l => l.LineItemDescr).Width(180).HeaderTemplate("Task");
            columns.Bound(l => l.LanguagePairID).Width(100).HeaderTemplate("Language Pair").ClientTemplate("<#= LanguagePair #>");
            columns.Bound(l => l.SourceWordCount).Width(100).HeaderTemplate("Source Words");            
            columns.Bound(l => l.QtyToInvoice).Width(100).HeaderTemplate("Quantity to Invoice");
            columns.Bound(l => l.Rate).Width(50).Format("${0:0.000}").HeaderHtmlAttributes(new { style = "text-align: center;" }).HtmlAttributes(new { style = "text-align: center;" });
            columns.Bound(l => l.SubTotal).Width(100).Format("{0:c}");
            columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Image);
                commands.Delete().ButtonType(GridButtonType.Image);
            }).Width(80).HtmlAttributes(new { style = "white-space: nowrap;" });
        })
        .Resizable(resizing => resizing.Columns(true))
        .Sortable()
        .Selectable()
        .Pageable(paging => paging.PageSize(10))
        .Scrollable(c => c.Height("233px"))
        .Filterable();

    StringWriter sw = new StringWriter();
    grid.Render();
    grid.WriteInitializationScript(sw);
}           

@Html.Hidden("InvoiceLineItemsGrid_tGrid", sw.GetStringBuilder().ToString())

Controller:

[GridAction]
public ActionResult _SelectInvoiceLineItems(int jobID)
{
    logLogic.logger.Debug(logLogic.GetMethodName() + ": User '" + User.Identity.Name);
    return View(new GridModel(jobLogic.GetInvoiceLineItems(jobID, User.Identity.Name)));
}

Thanks in advance for any insight.
Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情勿念 2024-12-13 08:12:40

看到 类似问题
我使用了 OnDataBinding 事件,如本文所述:
传递参数从 AJAX 绑定调用到控制器

对我有用。使用 Telerik 扩展 2012 年第 2 季度。

Saw a similar question on Teleriks forum.
I used OnDataBinding event as explained in this article:
On passing parameters to controller from AJAX binding call.

Works for me. Using Telerik extensions 2012 Q2.

海之角 2024-12-13 08:12:40

尝试为您想要(需要)在控制器上自动获取的每个 dataKey 指定 .RouteKey("someName"),"someName" 应与操作参数名称相同。

Try specifying .RouteKey("someName") for each dataKey that you want(need) to get automatically on controller, "someName" should be same with action parameter name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文