How to pass additional parameter to server-side while Performing CRUD operations?
This Knowledge base explains how to pass additional parameters to server side while performing CRUD operations in Grid.
Solution
This can be achieved using actionBegin event of ejGrid. In this event when edited record is saved the value is passed to the controller using “addParams” property of query API of ejGrid.
1.Render the grid control
JS
<div id="Grid"></div> var records= ej.dataManager.Raw(Json.Encode(Viewbag.dataSource)); <script type="text/javascript"> $(function () { $("#Grid").ejGrid({ dataSource: ej.DataManager({ json: records, adaptor: new ej.remoteSaveAdaptor(), updateUrl:"Home/NormalUpdate", insertUrl:"Home/NormalInsert",removeUrl: "Home/NormalDelete" }), allowPaging: true, editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true}, toolbarSettings: { showToolbar: true, toolbarItems: ["add","edit","delete", "update","cancel"]}, columns: [ { field: "OrderID", headerText: "Order ID", isPrimaryKey: true, width:” 80” }, { field: "EmployeeID", headerText: "Employee ID", width:” 80”}, { field: "CustomerID", headerText: "Customer ID", width:” 70” }, { field: "ShipCountry", headerText: "Ship Country", width:” 70” }, ], actionBegin:"begin", }); }); </script>
RAZOR
@(Html.EJ().Grid<object>("ViewGrid") .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).UpdateURL("/Home/NormalUpdate").InsertURL("/Home/NormalInsert").RemoveURL("/Home/NormalDelete").Adaptor(AdaptorType.RemoteSaveAdaptor)) .AllowPaging() .EditSettings(edit => { edit.AllowEditing().AllowAdding().AllowDeleting();}) .ToolbarSettings(toolbar => { toolbar.ShowToolbar().ToolbarItems(items => { items.AddTool(ToolBarItems.Add); items.AddTool(ToolBarItems.Edit); items.AddTool(ToolBarItems.Delete); items.AddTool(ToolBarItems.Update); items.AddTool(ToolBarItems.Cancel); }); }) .Columns(col => { col.Field("OrderID").IsPrimaryKey(true).HeaderText("Order ID").Width(80).Add(); col.Field("EmployeeID").HeaderText("Employee ID").Width(80).Add(); col.Field("CustomerID").HeaderText("Customer ID").Width(70).Add(); col.Field("ShipCountry").HeaderText("Ship Country").Width(70).Add(); }) .ClientSideEvents(e => e.ActionBegin("begin")) )
C#
namespace EntityCodeFirst.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { List<EditableOrder> data = OrderRepository.GetAllRecords().ToList(); ViewBag.datasource = OrderRepository.GetAllRecords().ToList(); return View(data); } } }
ASP.NET:
<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="true"> <DataManager Adaptor="remoteSaveAdaptor" UpdateURL="Default.aspx/Update" InsertURL="Default.aspx/Add" RemoveURL="Default.aspx/Delete" /> <ClientSideEvents ActionBegin="begin" /> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" Width="80"></ej:Column> <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="80"></ej:Column> <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="70"></ej:Column> <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="70"></ej:Column> </Columns> <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True"></EditSettings> <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings> </ej:Grid>
ASP.NET CORE
<ej-grid id="Grid" allow-paging="true" action-begin="begin"> <e-datamanager json="(IEnumerable<object>)ViewBag.datasource" adaptor="remoteSaveAdaptor" insert-url="NormalInsert" update-url="NormalUpdate" remove-url="NormalDelete"></e-datamanager> <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings> <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"add","edit","delete","update","cancel"}' /> <e-columns> <e-column field="OrderID" header-text="Order ID" is-primary-key width="80" ></e-column> <e-column field="EmployeeID" header-text="Employee ID" width="80" ></e-column> <e-column field="CustomerID" header-text="Customer ID" width="70" ></e-column> <e-column field="ShipCountry" header-text="Ship Country" width="70" ></e-column> </e-columns> </ej-grid>
Angular:
<ej-grid [dataSource]="gridData" allowPaging="true" [toolbarSettings]="toolbarItems" [editSettings]="editSettings" (actionBegin)="actionbegin($event)" > <e-columns> <e-column field="OrderID" headerText="Order ID" [isPrimaryKey]="true" width="80"></e-column> <e-column field="EmployeeID" headerText="Employee ID" width="80"></e-column> <e-column field="CustomerID" headerText="Customer ID" width="70"></e-column> <e-column field="ShipCountry" headerText="Ship Country" width="70"></e-column> </e-columns> </ej-grid>
TS File:
@ViewChild('grid') GridModel: EJComponents<any, any>; export class GridComponent { public gridData: any; public editSettings; public toolbarItems; constructor() { this.gridData = ej.DataManager({ json: window.gridData;, updateUrl: "/Grid/Update", insertUrl: "/Grid/Insert", deleteUrl: "/Grid/Delete", adaptor: new ej.remoteSaveAdaptor() }); this.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true}; this.toolbarItems = { showToolbar : true, toolbarItems : ["add", "edit", "delete", "update", "cancel"]}; } actionbegin(args:any){ if (args.requestType == "save" || args.requestType == "delete") { var value = "Value"; args.model.query.addParams("val", value); } } }
2. In actionBegin event we check the condition with the requestType and passing the value through addParams property of query API
JS
<script type="text/javascript"> function begin(args) { if (args.requestType == "save" || args.requestType == "delete") { var value = "Value"; args.model.query.addParams("val", value); } } </script>
Result:
Figure 1: Value passed through addParams and gets in the controller.