We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Save data to server

Hi,

i try to save my data to the server but its not working
please help.


CSHTML file:

@model IEnumerable<SyncfusionMvcCrud1.Customer>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@(Html.EJ().Grid<Customer>("ExportGrid")
.Datasource(ds => { ds.URL("/Home/BatchData").BatchURL("/Home/BatchUpdate").Adaptor(AdaptorType.UrlAdaptor); })
.AllowPaging()
.EditSettings(e => e.AllowEditing().AllowDeleting().AllowAdding().EditMode(EditMode.Batch))
.ToolbarSettings(tools => tools.ShowToolbar().ToolbarItems(item =>
{
item.AddTool(ToolBarItems.Add);
item.AddTool(ToolBarItems.Edit);
item.AddTool(ToolBarItems.Delete);
item.AddTool(ToolBarItems.Update);
item.AddTool(ToolBarItems.Cancel);
}))

.Columns(col =>
{

col.Field("CustomerID").IsPrimaryKey(true).Add();
col.Field("CompanyName").Add();
col.Field("City").Add();
col.Field("Country").Add();

}))



Controller file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SyncfusionMvcCrud1.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult BatchData()
{
var cust = new NORTHWNDEntities().Customers.ToList();
ViewBag.datasource = cust;
return View(cust);

}



public ActionResult BatchUpdate(string key, List<Customer> changed, List<Customer> added, List<Customer> deleted)
{

NORTHWNDEntities db = new NORTHWNDEntities();

//Performing insert operation
if (added != null && added.Count() > 0)
{
foreach (var temp in added)
{
db.Customers.Add(temp);
}
}

////Performing update operation
if (changed != null && changed.Count() > 0)
{
foreach (var temp in changed)
{
Customer old = db.Customers.Where(o => o.CustomerID == temp.CustomerID).SingleOrDefault();
if (old != null)
{
db.Entry(old).CurrentValues.SetValues(temp);
}
}
}

//Performing delete operation
if (deleted != null && deleted.Count() > 0)
{
foreach (var temp in deleted)
{
db.Customers.Remove(db.Customers.Where(o => o.CustomerID == temp.CustomerID).SingleOrDefault());
}
}

db.SaveChanges();


return RedirectToAction("BatchData");
}


}
}


Error was:

Server Error in '/' Application.

The resource cannot be found.



1 Reply

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team January 4, 2017 09:39 AM UTC

Hi Martin,  
 
Thanks for contacting Syncfusion Support.  
 
In the given code example, the controller page is different from the URL path given to the Grid. The paths are URL = “/Home/BatchData” and BatchURL = “/Home/BatchUpdate” but the controller name is not Home. BatchUpdate and BatchData are the methods present in the CustomerController not in the HomeController but you have given the URL path differently which is the root cause of the problem. But we wonder, how you have bind the data to the Grid with the URL path as “/Home/BatchData” which is completely wrong. Since the controller name is CustomerController, you cannot get the BatchData from HomeController (“/Home/BatchData”). Refer to the following code example for your reference. 
 
namespace MvcApplication66.Controllers 
{ 
    public class HomeController : Controller 
    { 
        public ActionResult Index() 
        { 
            return View(); 
        } 
        public ActionResult BatchUpdate(string key, List<EditableOrder> changed, List<EditableOrder> added, List<EditableOrder> deleted) 
        { 
                 . . 
        } 
        public ActionResult BatchData(Syncfusion.JavaScript.DataManager dm) 
        { 
               . . . 
        } 
    } 
} 
 
@(Html.EJ().Grid<OrdersView>("BatchEditing") 
        .Datasource(ds => 
            ds.URL("/Home/BatchData") 
            .BatchURL("/Home/BatchUpdate") 
            .Adaptor(AdaptorType.UrlAdaptor)) 
                 . . . 
                .. .  
) 
 
 
 
We also could see you are returning a View() as a result for the BatchData (Read POST of DataManager). So we suggest to modify the BatchData() as follows. 
 
@(Html.EJ().Grid<OrdersView>("BatchEditing") 
        .Datasource(ds => 
            ds.URL("/Home/BatchData") 
            .BatchURL("/Home/BatchUpdate") 
            .Adaptor(AdaptorType.UrlAdaptor)) 
                 . . . 
                .. .  
) 
 
        public ActionResult BatchUpdate(string key, List<EditableOrder> changed, List<EditableOrder> added, List<EditableOrder> deleted) 
        { 
            if (added != null) 
                OrderRepository.Add(added); 
                     . . . 
            return RedirectToAction("BatchData"); 
        } 
        public ActionResult BatchData(Syncfusion.JavaScript.DataManager dm) 
        { 
            IEnumerable Data = OrderRepository.GetAllRecords().ToList(); 
            DataResult result = new DataResult(); 
            DataOperations operation = new DataOperations(); 
            result.count = Data.AsQueryable().Count(); 
            if (dm.Skip != null) 
                Data = operation.PerformSkip(Data, dm.Skip); 
            if (dm.Take != null) 
                Data = operation.PerformTake(Data, dm.Take); 
            result.result = Data; 
            return Json(result, JsonRequestBehavior.AllowGet); 
        } 
 
 
We have prepared a sample that can be downloaded from the following location. 
 
 
Refer to the following Help Documents. 
 
 
If you are still facing any problem, please provide the following information to analyze the problem at our end. 
 
1)      Complete Code example of Grid and Controller 
2)      Complete Stack trace of the error produced 
3)      If possible, modify the attached sample and replicate the issue 
 
Regards, 
Seeni Sakthi Kumar S. 


Loader.
Live Chat Icon For mobile
Up arrow icon