We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

ASP.NET Core scaffolding and crud operations in GRID

I am trying to delete a record using the code generated during the scaffolding process. I take the basic table layout replace it with your grid and bind my model to the grid. See code below:

<ej-grid id="FlatGrid" allow-sorting="true" allow-paging="true">
    <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true"></e-edit-settings>
    <e-toolbar-settings show-toolbar="true" toolbar-items='@new List<string> {"add","edit","update","delete","cancel"}' />
    <e-datamanager json="(IEnumerable<object>)Model" update-url="/MedHistories/Edit" insert-url="/MedHistories/Create" remove-url="/MedHistories/Delete" adaptor="remoteSaveAdaptor"/>

    <e-columns >
        <e-column field="IdRecordMedHistory" is-primary-key="true" header-text="Id Record" text-align="Left" width="75" header-text-align="TextAlign.Center"></e-column>
        <e-column field="DonorId" header-text="Donor ID" text-align="Left" width="75" header-text-align="TextAlign.Center"></e-column>
        <e-column field="AdmisionDate" header-text="Admision Date" text-align="Center" format="{0:MM/dd/yyyy}" width="80" header-text-align="TextAlign.Center"></e-column>
        <e-column field="DonorHospitalName" header-text="Donor Hospital Name" text-align="Left" width="75" header-text-align="TextAlign.Center"></e-column>
        <e-column field="Age" header-text="Age" text-align=Left width="75" header-text-align="TextAlign.Center"></e-column>
        <e-column header-Text="Manage Records" width="100">
            <e-column-commands>
                <e-column-command type="edit">
                    <e-button-options content-type="TextOnly" text="Edit"></e-button-options>
                </e-column-command>
                <e-column-command type="delete">
                    <e-button-options content-type="TextOnly" text="Delete"></e-button-options>
                </e-column-command>
                <e-column-command type="save">
                    <e-button-options content-type="TextOnly" text="Save"></e-button-options>
                </e-column-command>
                <e-column-command type="cancel">
                    <e-button-options content-type="TextOnly" text="Cancel"></e-button-options>
                </e-column-command>
            </e-column-commands>
        </e-column>
    </e-columns>
</ej-grid>

The server side generated code look like this:

        public async Task<IActionResult> Delete(long? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var medHistory = await _context.MedHistory.SingleOrDefaultAsync(m => m.IdRecordMedHistory == id);
            if (medHistory == null)
            {
                return NotFound();
            }

            return View(medHistory);
        }

Using the tabular layout generated during scaffolding the following tag helpers were generated and it works.

<a asp-action="Delete" asp-route-id="@item.IdRecordMedHistory">Delete</a>

I have to be missing something in the configuration of the grid. Because it is not executing any of the crud operations. I want to replicate the functionality :

<a asp-action="Delete" asp-route-id="@item.IdRecordMedHistory">Delete</a> using your grid methods without changing the generated code. Is that possible?

Thanks in advance.




4 Replies

LE Leon April 10, 2017 08:25 PM UTC

Update:The generated code during scaffolding is using Async programming. I updated my code and now when I click on the delete button in the grid i am hitting the delete function on the server side. However in debug mode once it gets to the SaveChangesAsync(): no errors... just switch to the browser screen and the record is not deleted. The code below is what I am using in the server side:        public async Task SF_Delete([FromBody]CRUDModel value)        {            var medHistory = await _context.MedHistory.SingleOrDefaultAsync(m => m.IdRecordMedHistory == int.Parse(value.Key.ToString()));  _context.MedHistory.Remove(medHistory);            await _context.SaveChangesAsync();            return Json(value);        }Same behavior in update and insert. It is getting better ... but is it possible there is an issue(bug) with the Async programming?


SA Saravanan Arunachalam Syncfusion Team April 12, 2017 01:02 PM UTC

Hi Juan, 
We have analyzed your query and we suspect that you have return the CRUDModel object from server while delete the record which may be the cause of the issue. So, we suggest you to return that the deleted entity object instead of return CRUDModel object. Please refer to the below code example. 
public async Task SF_Delete([FromBody]CRUDModel value) 
        { 
            var medHistory = await _context.MedHistory.SingleOrDefaultAsync(m => m.IdRecordMedHistory == int.Parse(value.Key.ToString())); 
            _context.MedHistory.Remove(medHistory); 
            await _context.SaveChangesAsync(); 
            return Json(medHistory); 
        } 
 
We have created a sample based on your requirement that can be downloaded from the below link. 
Regards, 
Saravanan A. 



LE Leon April 13, 2017 03:43 AM UTC

Yes after this change... delete is working. Thanks for your help.


SA Saravanan Arunachalam Syncfusion Team April 14, 2017 06:26 AM UTC

Hi Juan,  
Thanks for your update.       
We are happy that the provided information helped you. 
Regards, 
Saravanan A. 


Loader.
Live Chat Icon For mobile
Up arrow icon