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

Json data not displaying properly in grid after save

5 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sid
Top achievements
Rank 1
Sid asked on 09 Sep 2014, 08:51 PM
I'm stumped.

I am trying to use Ajax batch editing, but when I go to save changes with a new record, the grid doesn't display the proper json data that is returned.

Here are a few screenshots to show what's happening:

Add a couple records in one batch, this seems to work great.


Now, if I try and add another record here is what happens:


It has posted to the database just fine.  I have a new line item with Id #35, part id #3, qty 3, and unit price 3, but instead it has duplicated the top record from the grid before the second posting.

Here's the raw json that was returned from the create method:


So, I don't know what I'm missing.  I've poked around but can't seem to pinpoint what is wrong.

Here is my view code:
@(Html.Kendo().Grid<PoLineItemModel>().Name("grdPoLines")
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(model => model.Id(m => m.Id))
            .Batch(true)
            .ServerOperation(false)
            .PageSize(20)
            .Events(events =>
            {
                events.RequestEnd("onRequestEnd");
                events.Change("onRequestEnd");
            })
            .Create(create => create.Action("Ajax_CreatePoLines","PoHeaders", new { poid = Model.Id}))
            .Read(read => read.Action("Ajax_ReadPoLines", "PoHeaders", new { poid = Model.Id }))
            .Update(update => update.Action("Ajax_UpdatePoLines", "PoHeaders", new { poid = Model.Id }))
            )
        .Columns(columns =>
        {
            columns.Bound(m => m.PoHeaderId).Visible(false);
            columns.Bound(m => m.Id);
            columns.Bound(m => m.PartId);
            columns.Bound(m => m.Quantity);
            columns.Bound(m => m.UnitPrice);
        })
        .ToolBar(toolbar =>
        {
            toolbar.Save();
            toolbar.Create();
        })
        .Navigatable()
        .Sortable()
        .Filterable()
        .Groupable()
        .Editable(edit => edit.Mode(GridEditMode.InCell))
)




Here is my controller method:


public ActionResult Ajax_CreatePoLines([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PoLineItemModel> poItems)
{
    var poid = Int32.Parse(Request.QueryString["poid"]);
    var ldb = new ShopTrackEntities();
 
    foreach (var newitem in poItems)
    {
        var newline = new PoLine
        {
            PoHeaderId = poid,
            PartId = newitem.PartId,
            Quantity = newitem.Quantity,
            UnitPrice = newitem.UnitPrice
        };
        ldb.PoLines.Add(newline);
    }
    ldb.SaveChanges();
 
    var allitems = GetPoLineItems(poid);
    var lines = new List<PoLineItemModel>();
 
    foreach (var item in allitems)
    {
        var additem = new PoLineItemModel
        {
            Id = item.Id,
            PoHeaderId = item.PoHeaderId,
            PartId = (int)item.PartId,
            Quantity = (int)item.Quantity,
            UnitPrice = (decimal)item.UnitPrice
        };
        lines.Add(additem);
    }
 
    return Json(lines.ToDataSourceResult(request,ModelState));
 
 
}

Any thoughts?

Thanks,
-Sid.

5 Answers, 1 is accepted

Sort by
0
Sid
Top achievements
Rank 1
answered on 09 Sep 2014, 11:58 PM
Sorry, didn't see that the screen shots didn't copy in the first post.

I've attached them - first save, posting two records, works fine.
second save, adding a third record,  posts to databse ok, sends back the proper json result, but the grid display is wrong.
0
Petur Subev
Telerik team
answered on 11 Sep 2014, 02:08 PM
Hелло ,

Please notice that only the items that were send for creation will be updated - the other records that already exist won't be updated even if you return them from the server. Could you please clarify what exactly is wrong on the pictures that you shared, since we are not really sure what exactly are the values that you send for create.

If you can create a small demo and share it with us we will troubleshoot the problem right away.

Kind Regards,
Petur Subev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sid
Top achievements
Rank 1
answered on 15 Sep 2014, 03:45 PM
Petur,

I am not having a problem with the data being saved.  That is working perfectly.  The "first save.png" attachment shows the grid after I have added two records and clicked save.  Works great, I get the two records I entered and they have Id's 36 & 37.  Now I try and add another record. If you look at the "fiddler json.png" attachment you'll see that there are three records with Id's of 36,37, & 38.   So, the record I added posted perfectly to the database and is being returned to the grid properly, but if you look at "second save.png" the records that are displayed are Id's 36,36, and 37.  So, how do I get the newest record to be displayed.  Does the grid know that I only added one record and is picking the first one that is returned in the json data?  How would I know which items to return to the grid if this is the case.  If you look at my Ajax_CreatePoLines method after I save the changes, I go back and grab all of the line items that have a PoHeader Id of the current PO # and return them to the grid.  Let me know if this is not how I should be doing it.  Hopefully that clarifies my problem.

Thanks,
-Sid.
0
Petur Subev
Telerik team
answered on 17 Sep 2014, 08:42 AM
Hello Sid,

Do you create those ID fields on the client or you create them on the server side? Please notice the kendo DataSource follows the idea that the IDs should be created on the server and then the updated recorods should be returned to the client the same explained in the link below - basically those ID fields should not be even editable.

http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/batch-editing (point 7)

In order to investigate what exactly happened (since we are not sure) we will need a small demo project to run and investigate on (feel free to pick up any code library project and modify it). Thank you for the understanding.

Kind Regards,
Petur Subev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sid
Top achievements
Rank 1
answered on 17 Sep 2014, 05:01 PM
Based on your post I did find the answer.  I don't mind reading, but I have found the Telerik documentation a bit lacking, especially when it comes to the MVC implementations, so it's not the first place I tend to look.  But based on my previous posts and the questions I asked I thought it might be a bit more clear than for me to have to post a demo project.

Here was my statement/question:
" So, how do I get the newest record to be displayed.  Does the grid know that I only added one record and is picking the first one that is returned in the json data?  How would I know which items to return to the grid if this is the case.  If you look at my Ajax_CreatePoLines method after I save the changes, I go back and grab all of the line items that have a PoHeader Id of the current PO # and return them to the grid.  Let me know if this is not how I should be doing it."

Documentation:
Comment in point 7 just above the JSON return statement.
"// Return the inserted entities. The grid needs the generated ProductID. Also return any validation errors."

So, I should have been only returning the inserted entities.

I do seem to have it working now.

-Sid.
Tags
Grid
Asked by
Sid
Top achievements
Rank 1
Answers by
Sid
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or