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

Data added to the grid twice, 3ce, 4ce etc

4 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
serge
Top achievements
Rank 2
Bronze
Iron
Iron
serge asked on 21 Apr 2021, 01:00 PM

I have a Core grid that manages the Sol objects. 

<div class="col-sm-12">
    @(Html.Kendo().Grid(Model)
                    .Name("mySolsGrid")
                    .Columns(columns =>
                    {
                        columns.Bound(c => c.Nom).Width(140);
                        columns.Bound(c => c.Code).Width(190);
                        columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
                    })
                    .ToolBar(
                        toolbar => {
                            toolbar.Create();
                            toolbar.Excel();
                        }
                    )
                    .Editable(editable => editable.Mode(GridEditMode.InLine))
                    .Pageable()
                    .Sortable()
                    .Scrollable(scr=>scr.Height(430))
                    .Filterable()
                    .DataSource(dataSource => dataSource
                        .Ajax()
                        .PageSize(20)
                        .Events(events => events.Error("error_handler"))
                        .Model(model => model.Id(p => p.Id))
                        .Create(update => update.Action("Sols_Create", "Sols"))
                        .Read(read => read.Action("Sols_Read", "Sols"))
                        .Update(update => update.Action("Sols_Update", "Sols"))
                        .Destroy(update => update.Action("Sols_Destroy", "Sols"))
                    )
                    )
</div>

This is my controller: 

[AcceptVerbs("Post")]
public async Task<ActionResult> Sols_CreateAsync([DataSourceRequest] DataSourceRequest request, SolDTO solDto)
{
    SolDTO idOnly = solDto;
    if (solDto != null && ModelState.IsValid)
    {
        idOnly = await _solService.CreateAsync(solDto);               
    }
 
    return Json(new[] { idOnly }.ToDataSourceResult(request, ModelState));
}

this is my Service returning object:

public async Task<SolDTO> CreateAsync(SolDTO solDto)
{
    var sol = _mapper.Map<Sol>(solDto);
    await _userService.SetCreatedByCurrentUserNowAsync(sol);
 
    sol = await _repository.AddAsync(sol);
    var dto = _mapper.Map<SolDTO>(sol);
    return dto;
}

I return here the ID that is not 0. However each time I add a new object (i click on update button) it calls the Create function one more time. First call once, second twice etc.

Does the Async method have something to do with that? 

4 Answers, 1 is accepted

Sort by
0
serge
Top achievements
Rank 2
Bronze
Iron
Iron
answered on 26 Apr 2021, 07:00 AM

I created Syncronous alternatives, but also fixed some async methods in all the solution that I didn't await... actually seems to work...

When using EF Core should always await all async methods...

0
Georgi Denchev
Telerik team
answered on 26 Apr 2021, 10:19 AM

Hello, Serge,

I am glad you found a solution and thank you for sharing it with the community!

Generally speaking the entire model should be returned to the Grid when a new row is added.

// Make sure the Id of the model is updated with the new value before returning the result.
return Json(new[] { solDto }.ToDataSourceResult(request, ModelState));

Demo of Grid CRUD operations with Inline editing:

https://demos.telerik.com/aspnet-core/grid/editing-inline

It's hard to tell what the exact cause of the problem is without debugging the project, however it is possible that the result is returned before the CreateAsync method has finished executing(due to the nature of how Asynchronous Programming works). In this case the default id of 0 would still be assigned to the row and whenever you add a new record, it'll be as if you have 2 rows with the same id.

Best Regards,
Georgi Denchev
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

0
serge
Top achievements
Rank 2
Bronze
Iron
Iron
answered on 26 Apr 2021, 11:30 AM

@Georgi, as a global suggestion, please in your demo code, give the feedback to the team, to synchronize the File names with the Class names. Is disturbing to see "Editing_InlineController.cs" with the class "GridController" defined in it... sometimes I search a Action "XXX" on a controller "Grid" but when I open Controllers folder I don't see any GridController, that is disturbing... 

 

Also, disturbing, when you passed from Inline editing to Batch editing, copied the same, apparently code, and it does not work because of.... "[Bind(Prefix = "models")]" that is not evident to remark at first time, also the collection of DTOs instead of a single DTO... such differences I believe should be noted in the explicative text, cause a lot of time I spent to debug, and there is any explicit error if instead of a single object you get the collection of objects... you just have the empty DTO object as parameter...

Thank you for the feedback ;)

0
Georgi Denchev
Telerik team
answered on 27 Apr 2021, 01:43 PM

Hello, Serge,

Thank you for the provided feedback!

The GridController which you see in the Source Code of the demo is a partial class that is why the name of the file and the name of the class are different. The reason multiple partial classes are used instead of one main Grid class is so that customers who download the local version of the demos can easily navigate to the Controllers folder and find the respective controller.

The Batch option is reserved for Incell editing as it's purpose is to submit all of the edited rows at once. That is why the Action expects a collection of items. With Inline and Popup modes a single row is saved at a time(when the Update button is clicked) so the action should expect a single item.

The information regarding Batch working with Cell editing is mentioned in the Batch Editing section of the documentation.

We are currently updating the links in the demos to point to the appropriate section of the documentation. Currently they all point to the overview page, after the update the Inline Demo will point to the Inline Documentation. The Template demo to the Template documentation and so on. We hope that this will help customers locate information faster.

Let me know if there is anything else I can assist you with or if you have any additional questions/feedback.

Best Regards,
Georgi Denchev
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

serge
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 27 Apr 2021, 06:22 PM

not clear for me the logic of naming... I have Url("ActionName", "ControllerName"), by eg. Url("BatchUpdate", "Grid"), in the demo code...so, I will seach where is the "Grid" controller, that, normally, should be named "GridController.cs"... I will never find any "GridController.cs", to see its "BatchUpdate" action...
Georgi Denchev
Telerik team
commented on 28 Apr 2021, 03:46 PM

Hi, Serge,

The files in the View Source tab are always related to the specific demo that is opened. When you are looking for the Controller code, it will always be the one showcased in the View Source tab. I'll speak to my colleagues regarding the naming in the widget itself. 

Tags
Grid
Asked by
serge
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
serge
Top achievements
Rank 2
Bronze
Iron
Iron
Georgi Denchev
Telerik team
Share this question
or