Clear added state after adding row in Batch Edit Mode

2 posts, 0 answers
  1. Shawn
    Shawn avatar
    51 posts
    Member since:
    Jul 2011

    Posted 31 Jul 2012 Link to this post

    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.
    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 does not. }
    	catch (Exception ex) {
                    ModelState.AddModelError("", ex.Message);
            return Json(ModelState.ToDataSourceResult());
  2. Shawn
    Shawn avatar
    51 posts
    Member since:
    Jul 2011

    Posted 01 Aug 2012 Link to this post

    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));
Back to Top