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.

Update with Entity Framework Issue

Thread ID:

Created:

Updated:

Platform:

Replies:

127009 Oct 20,2016 02:51 PM Feb 1,2017 07:44 AM ASP.NET MVC 5
loading
Tags: Grid
Michael Lambert
Asked On October 20, 2016 02:51 PM

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



Jayaprakash Kamaraj [Syncfusion]
Replied On October 21, 2016 09:03 AM

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. 


Michael Lambert
Replied On October 24, 2016 01:09 PM

Worked, Thanks!

Jayaprakash Kamaraj [Syncfusion]
Replied On October 25, 2016 01:03 AM

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. 


Michael Lambert
Replied On January 31, 2017 06:03 PM

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.

Farveen Sulthana Thameeztheen Basha [Syncfusion]
Replied On February 1, 2017 07:44 AM

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. 


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.

;