Edit Update and Delete based on HttpClient

Hi,

based on this example


I have a similar situation, I'm getting data from a webAPI project by:

 public class TicketListBase : ComponentBase
    {
        [Inject]
        public ITicketService TicketService { get; set; }

        public static IEnumerable<Ticket> Tickets { get; set; }
        public Ticket Ticket { get; set; }

        protected override async Task OnInitializedAsync()
        {
           Tickets =  (await TicketService.GetTickets()).ToList();
        }
    }


Ticket class as in web API model:
For simplicity, Ticket model is simplified.

public partial class Ticket
    {
        public int Id { get; set; }
        public string CustomerName { get; set; }
     
    }

TicketService:

public class TicketService : ITicketService
    {
        private readonly HttpClient httpClient;

        public TicketService(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }

        public async Task<Ticket> GetTicketById(int id)
        {
            return await httpClient.GetJsonAsync<Ticket>($"api/tickets/{id}");
        }

        public async Task<IEnumerable<Ticket>> GetTickets()
        {
            return await httpClient.GetJsonAsync<Ticket[]>("api/tickets");
        }
        public async Task<Ticket> UpdateTicket(Ticket updatedTicket)
        {
            return await httpClient.PutJsonAsync<Ticket>($"api/tickets/", updatedTicket);
            
        }
       
}


On blazor form:

 <SfGrid DataSource="@Tickets" AllowPaging="true">
            <GridColumns>
                <GridColumn Field=@nameof(Ticket.Id) HeaderText="#" TextAlign="TextAlign.Left" Width="80"></GridColumn>
                <GridColumn Field=@nameof(Ticket.CustomerName) HeaderText="Customer Name" Width="150"></GridColumn>
            </GridColumns>
</SfGrid>

And it works, but how to implement Edit,Update and Delete in this scenario.

For example, when I want to update my Ticket I would call

var result = await TicketService.UpdateTicket(Ticket);

where the Ticket would be current selected ticket on my page.

How to pass Grid changed ticket (I think I will go with Dialog approach) to my TicketService.

Best regards



6 Replies 1 reply marked as answer

KM Kuralarasan Muthusamy Syncfusion Team June 29, 2020 11:34 AM UTC

Hi df, 

Thanks for contacting Syncfusion support. 

From your query, we found that you need to handle the grid CRUD action with your API service. We suggest you to use the Grid OnActionBegin event to achieve this requirement. You can call your update function within this event like as below code snippet. 

<SfGrid @ref="Grid" DataSource="@Data" TValue="OrderDetails" Toolbar="@(new List<string> {"Add","Edit","Delete","Update","Cancel","Search" })" AllowPaging="true"> 
    <GridEvents OnActionBegin="OnActionBegin" TValue="OrderDetails"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
 
                    ... 
 
</SfGrid> 
 
@code{ 
    SfGrid<OrderDetails> Grid { get; set; } 
    List<OrderDetails> Data { get; set; } 
    protected override async Task OnInitializedAsync() 
    { 
        Data = await OrderData.GetPeople(); 
    } 
    public async void OnActionBegin(ActionEventArgs<OrderDetails> Args) 
    { 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save) 
        { 
            if (Args.Action == "add") 
            { 
                await OrderData.InsertOrderAsync(Args.Data); // handle add action here 
            } 
            else 
            { 
                await OrderData.UpdateOrderAsync(Args.Data.OrderID, Args.Data); // handle edit action here 
            } 
        } 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete) 
        { 
            await OrderData.DeleteOrderAsync(Args.Data.OrderID); // handle delete action here 
        } 
    } 
} 

Documentation for OnActionBegin event. 


For your convenience, we have prepared a sample with this requirement and you can find it from the below link. 


Regards, 
Kuralarasan M 


Marked as answer

DF df June 29, 2020 05:00 PM UTC

Hi,
thanks for the demo, it works after edit it triggers the OnActionEvent method, and sends the data to my API and data is saved on the server.
But now I have a stupid problem after that Modal edit window doesn't close, it stays open. I even tried with inline editing,
the same thing, data saved, but it doesn't exit from edit mode.
There is no error shown, only in modal case, the window doesn't close and in the case of inline, it stays in edit mode.

Also I tried with API line commented but same thing.

In your demo it works Ok, really don't understand the problem. I bet it's something so simple but I can't see it.

Also I noticed that OnActionComplete, never triggers. In your demo yes




DF df replied to Dubravko Fistric June 29, 2020 05:04 PM UTC

Hi,
thanks for the demo, it works after edit it triggers the OnActionEvent method, and sends the data to my API and data is saved on the server.
But now I have a stupid problem after that Modal edit window doesn't close, it stays open. I even tried with inline editing,
the same thing, data saved, but it doesn't exit from edit mode.
There is no error shown, only in modal case, the window doesn't close and in the case of inline, it stays in edit mode.

Also I tried with API line commented but same thing.

In your demo it works Ok, really don't understand the problem. I bet it's something so simple but I can't see it.

Also I noticed that OnActionComplete, never triggers. In your demo yes



Forgot to attach razor page and cs.

Attachment: Pages_d3f6f5dd.7z


KM Kuralarasan Muthusamy Syncfusion Team June 30, 2020 07:04 AM UTC

Hi df, 

From the given code, we found that you didn’t provide a primary key column to Grid. Primary key is must for the Grid editing feature. So to resolve this issue, please provide a primary key column by enabling the IsPrimaryKey (like as below code snippet) property in column level. 

<SfGrid @ref="Grid" DataSource="@Data" TValue="OrderDetails" Toolbar="@(new List<string> {"Add","Edit","Delete","Update","Cancel","Search" })" AllowPaging="true"> 
    <GridEvents OnActionBegin="OnActionBegin" OnActionComplete="OnActionComplete" TValue="OrderDetails"></GridEvents> 
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="Syncfusion.Blazor.Grids.EditMode.Dialog"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field="@nameof(OrderDetails.OrderID)" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field="@nameof(OrderDetails.CustomerID)" HeaderText="Customer ID" Width="150"></GridColumn> 
   </GridColumns> 
</SfGrid> 

Refer the below documentation to know more details about the Grid editing feature and IsPrimaryKey property. 



Regards, 
Kuralarasan M 



DF df June 30, 2020 08:20 AM UTC

Hi,
it works, I'm an idiot, with all the fiddling around I managed to delete IsPrimaryKey="true" property.
Thank you very much for your time and support.
Small question, for future reference, how do you paste your code so it's color-coded and formatted as in your replies.

Best regards,


KM Kuralarasan Muthusamy Syncfusion Team July 1, 2020 11:25 AM UTC

Hi df, 

We currently don’t have the support for code snippet option in the forums RTE. We have already logged a feature request for the same and will rollout this changes in our upcoming website release. As a workaround, you can copy the code from your IDE to your outlook mail and paste that content from the outlook mail to the RTE to give the code snippet as formatted, refer the screenshot below. 

 

Regards, 
Kuralarasan M 


Loader.
Up arrow icon