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

Adds, Edits, and Deletes are not save to database

I'm a newb so I apologize for my ignorance in advance.

I used the wizard to create an asp.net MVC grid view of my employee table.  I was able to click the links to edit/delete/add employee records.  I then removed that grid and replaced it with Razor code.  The grid is returned with the correct data, but when I try to add, edit, or delete records, it does not save the changes.  I get no error messages. 


Employee Index.cshtml code:

@model IEnumerable<MVC_Test.Models.tbl_Employee>

@{
    ViewBag.Title = "Manage Employees";
}

<h2>Manage Employees</h2>

@(Html.EJ().Grid<Object>("FlatGrid")
            .Datasource((IEnumerable<object>)ViewBag.DataSource)
            .AllowPaging()
            .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);
                });
            })
    .Columns(col =>
    {
        col.Field("EmployeeId").AllowEditing(false).IsIdentity(true).HeaderText("Employee ID").IsPrimaryKey(true).Add();
        col.Field("LastName").HeaderText("Last Name").Add();
        col.Field("FirstName").HeaderText("First Name").Add();
        col.Field("MI").HeaderText("Middle Initial").Add();
        col.Field("Suffix").HeaderText("Suffix").Add();
        col.Field("FullNameSort").AllowEditing(false).HeaderText("Full Name").Add();
    })
    )


EmployeeController.cs Code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVC_Test.Models;

namespace MVC_Test.Controllers
{
    public class EmployeeController : Controller
    {
        private MVCGridTest db = new MVCGridTest();

        // GET: Employee
        public ActionResult Index()
        {

            var DataSource = db.tbl_Employee;
            ViewBag.DataSource = DataSource;
            return View(db.tbl_Employee.ToList());
        }

3 Replies

TS Thavasianand Sankaranarayanan Syncfusion Team September 20, 2017 01:17 PM UTC

Hi Andrew, 

Thanks for contacting Syncfusion support. 

We have analyzed your query and we suspect that you want to save the edited value in the database also. So, we suggest you to use the RemoteSave adaptor in your sample and make the CRUD operations in the server side. 
 
Refer the below code example. 
 
[Index.cshtml] 
 
@(Html.EJ().Grid<object>("FlatGrid") 
         .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.datasource).UpdateURL("/Grid/Update"). 
         InsertURL("/Grid/Insert").RemoveURL("/Grid/Remove").Adaptor(AdaptorType. RemoteSaveAdaptor)) /* enabling the remote save adaptor */ 
 
        .AllowPaging()    /*Paging Enabled*/ 
        .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); }) 
        .ToolbarSettings(tool => { 
            tool.ShowToolbar().ToolbarItems(toolitem => 
            { 
                toolitem.AddTool(ToolBarItems.Add); 
                toolitem.AddTool(ToolBarItems.Delete); 
                toolitem.AddTool(ToolBarItems.Edit); 
                toolitem.AddTool(ToolBarItems.Update); 
                toolitem.AddTool(ToolBarItems.Cancel); 
            }); 
         
        }) 
        .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); 
             
            ------------- 
 
        })) 
 
 
 
[HomeCOntroller.cs] 

        public ActionResult Update(Orders value)  
        { 
            var ds = order.ToList();   //Update the modified record in datasource 
            Orders result = order.Where(o => o.OrderID == value.OrderID).FirstOrDefault(); 
            if (result != null) 
            { 
                result.OrderID = value.OrderID; 
                result.CustomerID = value.CustomerID; 
                result.ShipCountry = value.ShipCountry; 
                result.ShipCity = value.ShipCity; 
                result.ShipAddress = value.ShipAddress; 
            } 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
 
       public ActionResult Insert(Orders value) 
       { 
           Random ran = new Random();  
           value.OrderID = ran.Next();// For isIdentity column, need to generate a new value while inserting new record. 
           order.Insert(0, value); //Add a new record in datasource 
           return Json(value, JsonRequestBehavior.AllowGet); 
            
       }     
                         
        public ActionResult Delete(int key) 
        {   
            Orders result = order.Where(o => o.OrderID == key).FirstOrDefault(); 
            order.Remove(result); //Delete a record in datasource       
            return Json(key, JsonRequestBehavior.AllowGet); 
        } 


Note: The column which is specified as isIdentity will be in read-only mode both while editing and adding a record. Also, auto incremented value is assigned to that isIdentity column. You can handle the isIdentity column at server-side while updating record to the database 

We have prepared a sample and it can be downloadable from the below location. 


Refer the help documentation. 



Regards, 
Thavasianand S. 



MY Mortada Yeassen replied to Thavasianand Sankaranarayanan October 29, 2022 07:43 PM UTC

The Sample project has a problem we can not add any controller it tell me no model class are availbe



PS Pon Selva Jeganathan Syncfusion Team October 31, 2022 03:50 PM UTC

Hi Mortada,


Thanks for the contacting syncfusion forum.


Query: The Sample project has a problem we can not add any controller it tell me no model class are availbe


We checked your query by preparing a sample, but we were unable to replicate the issue at our end.


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_-_EJ1_CRUD-1451633264


If you still facing issue, please provide the following information


  1. Detailed explanation of your issue
  2. Complete grid code example
  3. Package version details
  4. Video demo/ screenshot of the issue
  5. Try to replicate the issue in our shared sample or share the issue reproducible sample.


The requested information will be helpful to proceed further


Regards,

Pon selva



Loader.
Live Chat Icon For mobile
Up arrow icon