This is a migrated thread and some comments may be shown as answers.

How to Pass Ajax Parameters?

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kevin
Top achievements
Rank 1
Kevin asked on 24 Sep 2011, 09:10 PM
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.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);
}          

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

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 26 Sep 2011, 11:48 AM
Hello Steve,

You can send additional values to the action method by configuring the Select method as described here. Those values will be evaluated on server and will be send every time the grid requests new data for actions like paging/sorting etc.

Other way of sending additional data to the server is by intercepting client OnDataBinding event where on demand you can send other params.

Bear in mind that the value set as ", new { jobID = "<#= JobID #>" will be used as is. This will not be evaluated on client as client template.

Greetings,
Nikolay Rusev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or