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

Update with Entity Framework Issue

I just starting out with MVC so sorry if I'm missing something obvious:

I want to use Entity Framework with ViewModels but I'm having a issue upon updating.

This issue I'm having is that the entity contains many more properties then are displayed in the grid, but when it goes to the Update Action the 'value' (entity) parameter only contains an entity where its values are only the ones displayed in the grid and nulls all the other values.   It seems like it only sends the Update Action a new entity with only values in the grid as opposed to the original entity with updated properties.  Is this by design?  I'd much rather have the original entity that contains changed values.

The view looks like this:
@model IEnumerable<SyncfusionMvcApplication1.Request>
@using SyncfusionMvcApplication1
@{
}
<h2>GridFeatures</h2>
<div id="ControlRegion">
    @(Html.EJ().Grid<Request>("FlatGrid")
                    .Datasource(ds => ds.Json(Model).UpdateURL("/Requests/Update").InsertURL("/Requests/Insert").RemoveURL("/Requests/Remove").Adaptor(AdaptorType.RemoteSaveAdaptor))
                    .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
                    .EnablePersistence(true)
        .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(p=>p.RequestID).HeaderText("Request ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field(p=>p.BU).HeaderText("BU").Width(80).Add();
            col.Field(p => p.NewExpand).HeaderText("New / Expand / Refresh").Width(80).EditType(EditingType.Dropdown).DataSource((List<KeyPair>)ViewBag.ddNewExp).Add();
    }))

The controller looks like this:
 public class RequestsController : Controller
    {
        private BudgetEntities db = new BudgetEntities();
        public ActionResult Grid()
        {
            return View(db.Requests.ToList());
        }
        public ActionResult Update(Request value)  //the value param only contains a entity with values that only appear in grid (the rest are null), NOT the original entity with updated values.
        {
            Request req = db.Requests.Single(o => o.RequestID == value.RequestID);
            db.Entry(req).CurrentValues.SetValues(value);
            db.Entry(req).State = EntityState.Modified;
            req.BU = value.BU;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }
            var result = db.Requests.ToList();
            return Json(new { result = result, count = result.Count }, JsonRequestBehavior.AllowGet);      
        }



5 Replies

JK Jayaprakash Kamaraj Syncfusion Team October 21, 2016 01:03 PM UTC

Hi Michael, 
 
Thank you for contacting Syncfusion support. 
 
The newly added/updated record details are bound to the ‘value’ parameter. In this parameter we will get only defined column values of Grid. To overcome this problem, in actionBegin event we need to pass all data for updated row in args.data when args.requestType as save. Please refer to the below code example and sample. 
 
@(Html.EJ().Grid<object>("FlatGrid") 
       .. 
 
           
        }).ClientSideEvents(eve=>eve.ActionBegin("actionbegin"))) 
<script type="text/javascript"> 
    function actionbegin(args) { 
        if (args.requestType == "save") { 
            args.data = ej.copyObject(this.model.currentViewData[args.selectedRow], args.data); 
       } 
    } 
</script> 

 
 
Regards, 
 
Jayaprakash K. 



ML Michael Lambert October 24, 2016 05:09 PM UTC

Worked, Thanks!


JK Jayaprakash Kamaraj Syncfusion Team October 25, 2016 05:03 AM UTC

Hi Michael,  
 
We are happy to hear that your issue has been resolved.  
 
Please get back to us if you need any further assistance.   
 
Regards,  
 
Jayaprakash K. 



ML Michael Lambert January 31, 2017 11:03 PM UTC

Actually this works fine for Updating a record, but when you try to Insert a record if copies the data from selectedindex of 0, not the newly created row.


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team February 1, 2017 12:44 PM UTC

Hi Michael, 


In the previous update, we have provided the solution to extend update records with the total number of fields in the dataSource. Since Grid will provide only the fields that are bound to the Grid, we have provided that solution. However, that solution is not needed to the new records. So we have prevented them in the ActionBegin event of the Grid.  


Please refer to the code example:- 

<script type="text/javascript"> 
    function actionbegin(args) { 
        if (args.requestType == "save" && (this.element.find(".e-addedrow").length==0)) { 
           args.data = ej.copyObject(this.model.currentViewData[args.selectedRow], args.data); 
        } 
    } 
</script> 
 



Please refer to the attached sample:- 



Regards, 

Farveen sulthana T. 


Loader.
Live Chat Icon For mobile
Up arrow icon