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.
Syncfusion Feedback

after add new record to grid and save fine, but IndexId is not updated?

Thread ID:

Created:

Updated:

Platform:

Replies:

129062 Feb 23,2017 10:48 PM UTC Mar 1,2017 08:44 AM UTC ASP.NET MVC 6
loading
Tags: Grid
Megatron
Asked On February 23, 2017 10:48 PM UTC

Hi can you help with why the ID is not updated on the client side.
  1. In the code below the, insert/add is successful with EF 6.1, but new record row id is not updated after POST actions..
  2. how to set the width automatically by length??
  3. how to let user perform multi record update on client, and how to get the correct records and save in controller.
  4. how to change this to load fully JSON and not viewbag ds => ds.Json((IEnumerable<Object>)ViewBag.dataSource

View Code

 @(Html.EJ().Grid<Employee>("Grid").Datasource(ds => ds.Json((IEnumerable<Employee>)ViewBag.dataSource).UpdateURL("/home/Update").InsertURL("/home/Insert").RemoveURL("/home/GRemove").Adaptor(AdaptorType.RemoteSaveAdaptor))

..
  .ToolbarSettings(t =>
        {
            t.ShowToolbar().ToolbarItems(i =>
            {
                i.AddTool(ToolBarItems.Add);
                i.AddTool(ToolBarItems.Edit);
                i.AddTool(ToolBarItems.Delete);
                i.AddTool(ToolBarItems.Update);
                i.AddTool(ToolBarItems.Cancel);
            });
        })
        .Columns(col =>
        {           
            col.Field("Id").HeaderText("Id").IsIdentity(true).IsPrimaryKey(true).Width(10).Visible(true).Add(); // how to set the width automatically by length??

---- Controller
       [HttpPost]
       [ValidateAntiForgeryToken]
        public async Task<ActionResult> Insert(Employee value) // if I use
        {              
            if (ModelState.IsValid)
            {
                db.Employee.Add(value);
                await db.SaveChangesAsync();
                var data = await db.employee.ToListAsync();     
                return Json(data, JsonRequestBehavior.AllowGet);                  

Thanks

    

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On February 24, 2017 12:06 PM UTC

Hi Megatron, 
 
Thanks for contacting Syncfusion Support.  
 
Query #1: 
 
We are able to reproduce the problem at our end with the given code example. The main reason behind the issue is the inserted recorded has not been returned to the client-side. Since you are using the IsIdentity for the PrimaryKey column, the value in the client-side differs from the server-side and to notify the changes on the IsIdentity column you have to return them from server-end. Refer to the following code example. 
 
        public ActionResult Insert(EditableOrder value) 
        { 
            Random ran = new Random(); 
            value.OrderID = ran.Next(); 
            OrderRepository.Add(value); 
            var data = OrderRepository.GetAllRecords(); 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
 
 
 
Query #2: how to set the width automatically by length 
 
We could see you would like to place the text of the columns fit to the given space/width and render the column based on header/content width. This can be achieved by enabling the AllowResizeToFit property of the Grid. Refer to the following code example and API Reference Section. 
 
@(Html.EJ().Grid<object>("Grid") 
       .. .  
    .AllowResizeToFit() 
           . . . 
                  . . . 
) 
 
 
 
Query #3: how to let user perform multi record update on client, and how to get the correct records and save in controller 
 
This can be achieved by using the Excel-like/Batch Editing feature of the Grid. Refer to the following Help Documents.  
 
 
To perform CRUD operations with the Batch Editing Enabled Grid, you have to use separate API (BatchURL). Refer to the following Help Document and code example. 
 
 
@(Html.EJ().Grid<object>("BatchEditing") 
    .Datasource(ds => 
        ds.Json((IEnumerable<object>)ViewBag.data) 
        .BatchURL("/Home/BatchUpdate") 
        .Adaptor(AdaptorType.RemoteSaveAdaptor)) 
 
    .EditSettings(edit => 
    { 
        edit.AllowAdding(); 
        edit.AllowEditing(); 
        edit.AllowDeleting(); 
        edit.EditMode(EditMode.Batch); 
    }) 
          ..  
) 
 
 
Query #4: how to change this to load fully JSON and not viewbag ds => ds.Json((IEnumerable<Object>)ViewBag.dataSource 
 
We suspect that you would like to populate the Grid using the on demand paging. This can be achieved using the URLAdaptor of the DataManager. Refer to the following code example.  
 
 
        public ActionResult DataSource(Syncfusion.JavaScript.DataManager dm) 
        { 
            IEnumerable datasource = GridData; 
            DataOperations operation = new DataOperations(); 
            //Each actions in Grid  must be handled here  
 
            if (dm.Where != null)//for filtering 
                datasource = operation.PerformWhereFilter(datasource, dm.Where, dm.Where[0].Condition); 
            int count = datasource.AsQueryable().Count(); 
            if (dm.Sorted != null)//for sorting 
                datasource = operation.PerformSorting(datasource, dm.Sorted); 
            if (dm.Skip != null)//for paging 
                datasource = operation.PerformSkip(datasource, dm.Skip); 
            if (dm.Take != null)//for paging 
                datasource = operation.PerformTake(datasource, dm.Take); 
            return Json(new { result = datasource, count = count }, JsonRequestBehavior.AllowGet); 
        } 
 
 
@(Html.EJ().Grid<object>("Grid") 
    .Datasource(ds => 
        ds.URL("/Home/DataSource") 
        .Adaptor(AdaptorType.UrlAdaptor)) 
            . ..  
) 
 
 
Please make a note that the result must be wrapped with result/count pair. Refer to the following Help Document and KB. 
 
 
We have prepared a sample that can be downloaded from the following location. 
 
 
Regards, 
Seeni Sakthi Kumar S. 


Megatron
Replied On February 24, 2017 08:32 PM UTC

Hi, thanks for the help.

Using J San & rem Adaptor if I do delete/remove with ef,

should i return entire list back or just deleted record? how does the grid update its index after delete? what should I return..

 public async Task<ActionResult> del(int id)
        {
            if (id <=0) // error check                        
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, id.ToString());
            Cup cup = await db.Cup .FindAsync(id);
            dbcup .Remove(cup );
            await db.SaveChangesAsync();   
            //    
            return Json(cup , JsonRequestBehavior.AllowGet);
        }

Megatron
Replied On February 24, 2017 08:49 PM UTC

Also should I check for less row than 1 or less than row <= 0 for valid row

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On February 27, 2017 09:59 AM UTC

Hi Megatron, 
 
Query #1: Using J San & rem Adaptor if I do delete/remove 
 
There is no need to return entire records to the client end after updating/deleting the records in the Grid. You can either return the (deleted/updated) data to Grid or prevent the data from the server end to client using void method as shown in the following code example. So the Grid will automatically update the value to the corresponding records in the client-end. 
 
        public ActionResult Delete(int key) 
        { 
            var value = OrderRepository.GetAllRecords().Where(e => e.OrderID == key).FirstOrDefault(); 
            OrderRepository.Delete(key); 
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
        //or  
        public void Delete(int key) 
        { 
            OrderRepository.Delete(key); 
        } 
 
Please make a note, if you are using IsIdentity column in the Grid, you have to return the updated record to the client end as explained in our previous update.  
 
Query #2: Also should I check for less row than 1 or less than row <= 0 for valid row 
 
Generally, a IsPrimary key column must hold a valid number. Otherwise, it will lead to some problems in the Grid editing. So ensure to update the record with the valid primarykey value for the respective records. So you don’t know need to ensure it back again in the server while deleting them.  
 
Regards, 
Seeni Sakthi Kumar S. 


Megatron
Replied On February 28, 2017 04:03 AM UTC

Thanks seethi. The delete is working fine now.



When I use the Random ran = new Random(); 
            value.OrderID = ran.Next();  in my insert when primary & idenity is turned on the Entity FRamework / Db are throwing error.

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On March 1, 2017 08:44 AM UTC

Hi Megatron,  
 
In our previous updates, for sample purpose, we have generated the random number and updated them to the IsIdentity (OrderID) Column in the Grid. If you have defined the IsIdentity column as Auto Generated in the DB, you don’t need to generate a Random number dynamically. While inserting the new records to the DB, the system will auto-generate the value for the PrimaryKey column. So we suggest to remove the Random number generation for the PrimaryKey column in your application. However, as suggested earlier, you have to return the newly added records to the client to inform the Grid about the changes in the PrimaryKey column.  
 
Regards, 
Seeni Sakthi Kumar 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.

Warning Icon 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.Close Icon

;