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.
Unfortunately, activation email could not send to your email. Please try again.

ASP.NET Core scaffolding and crud operations in GRID

Thread ID:

Created:

Updated:

Platform:

Replies:

129930 Apr 10,2017 02:06 PM Apr 14,2017 02:26 AM ASP.NET Core 4
loading
Tags: Grid
Leon
Asked On April 10, 2017 02:06 PM

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.




Leon
Replied On April 10, 2017 04:25 PM

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?

Saravanan Arunachalam [Syncfusion]
Replied On April 12, 2017 09:02 AM

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. 


Leon
Replied On April 12, 2017 11:43 PM

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

Saravanan Arunachalam [Syncfusion]
Replied On April 14, 2017 02:26 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;