Category / Section
How to send current row data to server using template column
1 min read
Problem:
How to pass column data to controller while using template column.
Solution:
While using template column if we want to pass the data to the controller from client side then we have to use “{{:}}” for getting the value from the database field.
MVC:
@(Html.EJ().Grid<SampleDemo.OrdersView>("FlatGrid") .Datasource((IEnumerable<object>)ViewBag.datasource) .AllowPaging() /*Paging Enabled*/ .Columns(col => { col.Field("OrderID").HeaderText("Order ID"). IsPrimaryKey(true).TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75).Add(); col.Field("CustomerID").HeaderText("Customer ID").Width(80).Add(); col.Field("EmployeeID").HeaderText("Employee ID"). TextAlign(System.Web.UI.WebControls.TextAlign.Right).Width(75).Add(); col.Field("ShipCountry").HeaderText("Ship Country").Width(80).Add(); col.Field("ShipCity").HeaderText(" ").Template("<a href='Events?orderID={{:OrderID}}&customerID={{:CustomerID}}&&employeeID={{:EmployeeID}}&shipCountry={{:ShipCountry}}'>View PO</a>").Add(); }))
ASP:
<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True"> <Columns> <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="Right" Width="75" /> <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="80" /> <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="75" /> <ej:Column Field="ShipCountry" HeaderText=" Ship Country " Width="80" /> <ej:Column HeaderText=" ShipCity" Template="<a href='Events?orderID={{:OrderID}}&customerID={{:CustomerID}}&&employeeID={{:EmployeeID}}&shipCountry={{:ShipCountry}}'>View PO</a>" Width="110" /> </Columns> </ej:Grid>
Here we have passed the OrderID, CustomerID, EmployeeID and ShipCountry value to the controller side “Events” method.
.CS
public class GridController : Controller { public void Events(int orderID, string customerID,int employeeID,string shipCountry) { //Code you want to do } }
Note: The controller method parameter value should be same as the passed value from the client side (as marked as Red color values).
Result: