Monday, August 13, 2012

Kendo UI with MVC: Passing parameters to your controller

<div class="SearchGridWrapper">
    @(Html.Kendo().Grid<PACT.DataModel.InventoryItems_Active>()
            .Name("ItemSearchGrid")
            .Columns(columns =>
            {
                columns.Bound(a => a.ItemId).Title("Part #");
                columns.Bound(a => a.ItemName).Title("Description");
                columns.Bound(a => a.InventManufacturerId).Title("Manufacturer");
                columns.Bound(a => a.ItemGroupId).Title("Category");
                columns.Bound(a => a.ModelGroupId).Title("Model Group");
            })
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(10)
                .Read(read => read.Action("Search", "ItemSearch")
                    .Data("ItemFormValues")
                )
                .ServerOperation(false)
               
            )
            .Selectable()
            .AutoBind(false)
            .Pageable()
            .Sortable()
            .Events(e => e.DataBound("DoubleClickItemGridRowData"))
        )
</div>

Notice the .Read:  The .Data calls a JQuery function that can pass parameters to the controller

function ItemFormValues() {
    return {
        ItemPartNumber: $("#ItemPartNumber").val(),
        ItemManufacturer: $("#ItemManufacturer").val(),
        ItemCategory: $("#ItemCategory").val(),
        DataAreaID: "mdsi",

    };
}

This will automatically send the parameters to the controller:

       public ActionResult Search(string ItemPartNumber, string ItemManufacturer, string ItemCategory, string DataAreaID, [DataSourceRequest]DataSourceRequest request)
        {
            IList<InventoryItems_Active> items = new List<InventoryItems_Active>();
            using (service)
            {
                    items = service.GetInventoryItems(ItemPartNumber, ItemManufacturer, ItemCategory, DataAreaID);
            }
            DataSourceResult result = items.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

No comments:

Post a Comment