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.
Unfortunately, activation email could not send to your email. Please try again.

Adds, Edits, and Deletes are not save to database

Thread ID:

Created:

Updated:

Platform:

Replies:

132763 Sep 19,2017 03:39 PM Sep 20,2017 09:17 AM ASP.NET MVC 1
loading
Tags: Grid
Andrew Rosenthal
Asked On September 19, 2017 03:39 PM

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());
        }

Thavasianand Sankaranarayanan [Syncfusion]
Replied On September 20, 2017 09:17 AM

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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;