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

Clear added state after adding row in Batch Edit Mode

1 Answer 244 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 2
Shawn asked on 31 Jul 2012, 04:03 PM
I am using a Kendo grid in Batch Edit Mode.  When I add rows to the grid and click Save Changes, the added rows correctly get passed to the Create method that I have hooked up, and I am able to add the rows to the database.  The problem I have is that when I then edit one of those newly-added rows and click Save Changes, these rows get passed to the Create method (again) instead of being passed to the Update method.
Please advise asap as to what I am doing wrong.  Here is my Create Method that is hooked up to the grid.
[HttpPost]
public ActionResult Create( [DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]FormCollection addedRoles) {
        try { // code here to update the database from the addedRoles FormCollection. This part works fine. ModelState.Clear() // I added this in to see if it makes a difference...it does not. }
	catch (Exception ex) {
                ModelState.AddModelError("", ex.Message);
            }
        return Json(ModelState.ToDataSourceResult());
}

1 Answer, 1 is accepted

Sort by
0
Shawn
Top achievements
Rank 2
answered on 01 Aug 2012, 03:23 PM
I found the solution.  Not in the Create method but in the Read method.
In the Read method, I was passing the grid a collection based on Csla.ReadonlyListBase.
But in the Create method, I was passing it a collection based on Csla.BusinessListBase.

So I changed my Read method to pass a collection based on Csla.BusinessListBase, and it broke, with a CircularReference error encountered in serializing the object to Json.

It seems Csla.BusinessListBase objects don't serialize well to Json.

But, in my Create method, I MUST use a BusinessListBase because I am editing a collection.  So what to do?!?

I solved the dilemma by creating a simple POCO that represents my object (kind of a ViewModel object), and in the Read method, I pass a List<RoleVM> that is populated from the retrieved collection.

Then in the Create method I do a similar thing after saving the newly-created roles collection to the database (stored in a variable called newRoles).  Then I return the following:
var list = new List<ViewModels.RoleVM>();
newRoles.ToList().ForEach(r => list.Add(new RoleVM {  RoleID = r.RoleID, Rolename = r.Rolename }));
return Json(list.ToDataSourceResult(request));
Tags
Grid
Asked by
Shawn
Top achievements
Rank 2
Answers by
Shawn
Top achievements
Rank 2
Share this question
or