This is a migrated thread and some comments may be shown as answers.

Save to database

2 Answers 79 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael
Top achievements
Rank 1
Michael asked on 13 Dec 2010, 11:38 AM
In the telerik example on the following link:
http://demos.telerik.com/aspnet-mvc/grid/editingajax

I was able to get it to work, but I am confused on how I am suppose save the changes to the database.  So, the original ActionResult _SaveAjaxEditing:

 public ActionResult _SaveAjaxEditing(int id)
       
{
           
EditableProduct product = SessionProductRepository.One(p => p.ProductID == id);
           
           
TryUpdateModel(product);
           
SessionProductRepository.Update(product);
           
return View(new GridModel(SessionProductRepository.All()));
       
}

I changed it to below, and it works, but I feel there is probably a better way.  The RefundEntities represents the entity framework model I have.  So, it looks like I needed to update the session and then update the database.  Would this be correct?  Any better way?

public ActionResult _SaveAjaxEditing(int id)       
        {
            using (RefundEntities db = new RefundEntities())
            {
                EditableClient client = SessionClientRepository.One(c => c.ClientID == id);
                var updateClient = db.tblClient.First(c => c.ClientID == id);
                TryUpdateModel(client);
                TryUpdateModel(updateClient);
                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                }
                SessionClientRepository.Update(client);
            }
           
            return View(new GridModel(SessionClientRepository.All()));       
        }    

2 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 13 Dec 2010, 01:03 PM
Michael,

In the sample you are referring to, the SessionProductRepository is the repository which is used as entities' storage (which for simplicity is using session for persistence). Therefore, in your production code, this repository should be replaced with code which to manipulate the data from your actual backend. Having this in mind, in the second snippet you have pasted, the use of the Session repository is not required

Regards,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Michael
Top achievements
Rank 1
answered on 13 Dec 2010, 02:05 PM

Rosen, thanks for the quick response and great explanation!

Michael

 

Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Michael
Top achievements
Rank 1
Share this question
or