Articles in this section
Category / Section

How to change the url action path for the Grid CRUD operations?

1 min read

While specifying the URL that is to be triggered on performing the CRUD operations, the action name is to be prepended with the controller name in order to avoid the routing issues.

Solution

In the dataSource property of the Grid, you can bind a URL link and thus obtain and process the data from the Server/Code-behind.

The various properties of the dataManager class are tabulated as follows.

Properties

Description

url

Used to fetch data from the remote dataSource

updateUrl

Used to update changes made in the database

insertUrl

Used to insert new data in the database

removeUrl

Used to remove a record in the database

batchUrl

Used to make batch changes in the database

crudUrl

Used when same method is to be triggered for all insert, update and remove operation

json

Used to fetch data from a local dataSource

adaptor: “jsonAdaptor

Used to process json data

adaptor: “UrlAdaptor

Used when binding data from a remote dataSource

adaptor: “remoteSaveAdaptor

Used when binding local data to the grid dataSource

adaptor: “ODataAdaptor

Used for consuming data from a OData service

adaptor: “WebApiAdaptor

Used for retrieving data from WebApi service

adaptor: “customAdaptor

Used to customize the adaptor available in dataManager class

Example

In the following code example, the dataSource is bound to the Grid from a remote data and an UrlAdaptor is used.

JS

<script type="text/javascript">  
       $(function () {                    
                $("#Grid").ejGrid({
                    dataSource: ej.DataManager({ url: "/Home/DataSource", updateUrl: "/Home/Update", insertUrl: "/Home/Insert", removeUrl: "/Home/Delete", adaptor: "UrlAdaptor" }),
                    allowPaging: true,
                    editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true },
                    toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },
                    columns: [
                            { field: "OrderID", isPrimaryKey: true, headerText: "Order ID" },                            
                            { field: "EmployeeID", headerText: 'Employee ID'},
                            { field: "Freight", headerText: 'Freight', format: "{0:C}"  },                            
                            { field: "ShipCity", headerText: 'Ship City'}
                    ] 
                });
        })     
    </script>

MVC

[In View]
@(Html.EJ().Grid<object>("Grid")
            .Datasource(ds => ds.URL(@Url.Action("DataSource", "Home", null, Request.Url.Scheme)).UpdateURL(@Url.Action("Update", "Home", null, Request.Url.Scheme)).InsertURL(@Url.Action("Insert", "Home", null, Request.Url.Scheme)).RemoveURL(@Url.Action("Delete", "Home", null, Request.Url.Scheme)).Adaptor(Adaptor.UrlAdaptor))
            .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
            .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);
                    });
             })             
            .AllowPaging()
            .Columns(col =>
                    {
                        col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add();                        
                        col.Field("EmployeeID").HeaderText("Employee ID").Add();
                        col.Field("Freight").Format("{0:c}").Add();
                        col.Field("ShipCity").HeaderText("Ship City").Add();                        
            })            
        )
[In controller]
namespace EJGrid.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult DataSource(Syncfusion.JavaScript.DataManager dm)
        {
            var DataSource = OrderRepository.GetAllRecords();
            DataResult result = new DataResult();
            result.result = DataSource.Skip(dm.Skip).Take(dm.Take).ToList();
            result.count = DataSource.Count();
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public class DataResult
        {
            public IEnumerable<editableorder>
    result { get; set; }
            public int count { get; set; }
        }
        public ActionResult Update(EditableOrder value)
        {
            OrderRepository.Update(value);
            var data = OrderRepository.GetAllRecords();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Insert(EditableOrder value)
        {
            OrderRepository.Add(value);
            var data = OrderRepository.GetAllRecords();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Delete(int key)
        {
            OrderRepository.Delete(key);
            var data = OrderRepository.GetAllRecords();
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}

ASP.NET

[aspx]
<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True" Create="create"> 
            <editsettings allowadding="True" allowediting="True" allowdeleting="True"></editsettings>
            <datamanager url="/Default.aspx/Data" updateurl="/Default.aspx/Update" inserturl="/Default.aspx/Add" removeurl="/Default.aspx/Delete" />
            <toolbarsettings showtoolbar="True" toolbaritems="add,edit,delete,update,cancel"></toolbarsettings>           
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True" />                
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" />
                <ej:Column Field="Freight" HeaderText="Freight" Format="{0:C}" />                
                <ej:Column Field="ShipCity" HeaderText="Ship City" />
            </Columns>
</ej:Grid>  
[CS]
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static object Data(int skip, int take)
    {
        var DataSource = OrderRepository.GetAllRecords();
        DataResult ds = new DataResult();
        ds.result = DataSource.Skip(skip).Take(take);
        ds.count = ds.count = DataSource.Count();
        return ds;
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static object Update(Order value)
    {
        NORTHWNDEntities1 obj = new NORTHWNDEntities1();
        obj.Entry(value).State = EntityState.Modified;
        obj.SaveChanges();
        var dataSource = OrderRepository.GetAllRecords();
        return dataSource;
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static object Delete(int key)
    {
        NORTHWNDEntities1 obj = new NORTHWNDEntities1();
        var data = obj.Orders.Find(key);
        obj.Orders.Remove(data);
        obj.SaveChanges();
        var dataSource = OrderRepository.GetAllRecords();
        return dataSource;
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static object Add(Order value)
    {
        NORTHWNDEntities1 obj = new NORTHWNDEntities1();
        obj.Orders.Add(value);
        obj.SaveChanges();
        var dataSource = OrderRepository.GetAllRecords();
        return dataSource;
    }
} 

Screenshot:

http://help.syncfusion.com/ug/js/ImagesExt/image48_60.png

Figure 1: Editing record

http://help.syncfusion.com/ug/js/ImagesExt/image48_61.jpg

Figure 2: Obtaining the values of edited record at server side

http://help.syncfusion.com/ug/js/ImagesExt/image48_62.jpg

Figure 3: Modified Grid with Post operation displayed in the bottom

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied