0

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

2
  • What isn't working? Is the parameter null or an empty string or something? Or are you getting some error? Commented Sep 24, 2011 at 23:23
  • Yes Matt is right, you need to be more specific. What is the value that you receive in your controller action. Since you are using paging, try to find out whether the request works for the first page and stops working when you go to another page. Also, there must be some pattern in which the you receive wrong values in action method. Commented Sep 25, 2011 at 4:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.