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

Using ViewModel with grid

I'm using ViewModel instead of Viewbag but need to understand how best to handle updates. In this snippet I am only using a single datasouce in the the model to keep it simple, but in the production I have several other data sources that I'll need to include, hence the ViewModel approach as opposed to several Viewbag definitions. The following code does work, though I can only send back the json code in the Update action (not a ViewModel). Need assistance with best practices on how best to handling working/updated with ViewModels.

Mike

View:
@(Html.EJ().Grid("TestGrid")
.Datasource(ds => ds.Json(Model).UpdateURL("/Requests/Update").InsertURL("/Requests/Insert").RemoveURL("/Requests/Remove").Adaptor(AdaptorType.RemoteSaveAdaptor))
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })

Controller:
public ActionResult Grid()
{
return View(db.Requests.ToList());
}

public ActionResult Update(Request value)
{
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;
db.SaveChanges();
var result = db.Requests.ToList();
return Json(new { result = result, count = result.Count }, JsonRequestBehavior.AllowGet);
}


1 Reply

JK Jayaprakash Kamaraj Syncfusion Team October 25, 2016 12:41 PM UTC

Hi Michael, 
 
Thank you for contacting Syncfusion support. 
 
While performing CRUD operation in Grid with IsIdentity column it is essential to return updated data in Json format from controller. Void type also supports so if you are not using isIdentity column in Grid Use Void type because it is not necessary to return updated data while using void type and also changes will be reflected automatically in database and in Grid. Please refer to the below code example.   
 
        //Perform update 
        public void PerformUpdate(OrderTable value) 
        { 
 
            db.Entry(value).State = EntityState.Modified; 
            db.SaveChanges(); 
        } 
 
Regards, 
 
Jayaprakash K. 


Loader.
Up arrow icon