Hi here is my code
@page "/"
@page "/ViewCountry"
@inject HttpClient http;
@inject NavigationManager navigationManager;
@inject IJSRuntime JSRuntime
<h3>Country</h3>
@if (countryList == null)
{
<div class="row">
<div class="col-md-12" id="container">
<SfSpinner ID="spinner" Type="Syncfusion.Blazor.Popups.SpinnerType.Material" Target="container" Width="200">
</SfSpinner>
</div>
</div>
}
else if (countryList.Count == 0)
{
<span>No record found in the Database.....</span>
}
else
{
<SfButton OnClick="AddNew">Add New</SfButton>
<br />
<SfGrid DataSource="@countryList" TValue="CountryMaster" AllowFiltering="true" AllowPaging="true" AllowSorting="true">
<GridEditSettings AllowDeleting="true" AllowEditing="true"
ShowDeleteConfirmDialog="true"></GridEditSettings>
<GridSearchSettings IgnoreCase="true" Operator="Operator.Contains"></GridSearchSettings>
<GridPageSettings PageSize="5"></GridPageSettings>
<GridEvents CommandClicked="CommandClickHandler" TValue="CountryMaster"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(CountryMaster.CountryName) HeaderText="Country Name"></GridColumn>
<GridColumn Width="150" Filter="false" HeaderText="Actions">
<GridCommandColumns>
<GridCommandColumn Type="CommandButtonType.Edit" ButtonOption="@(new
CommandButtonOptions() {IconCss="e-icons e-edit", CssClass="e-flat"
})"></GridCommandColumn>
<GridCommandColumn Type="CommandButtonType.Delete"
ButtonOption="@(new CommandButtonOptions() {IconCss="e-icons e-delete",
CssClass="e-flat" })"></GridCommandColumn>
</GridCommandColumns>
</GridColumn>
</GridColumns>
</SfGrid>
}
@code {
public List<CountryMaster> countryList { get; set; }
string apiPath = "https://localhost:44321/api/" + "CountryMaster";
protected override async Task OnInitializedAsync()
{
await LoadCountryList();
}
async Task LoadCountryList()
{
countryList = await http.GetJsonAsync<List<CountryMaster>>(apiPath);
}
public void CommandClickHandler(CommandClickEventArgs<CountryMaster> args)
{
long CountryId = 0;
if (args.CommandColumn.Type == CommandButtonType.Edit)
{
CountryId = args.RowData.CountryId;
navigationManager.NavigateTo("/EditCountry/" + CountryId);
}
else if (args.CommandColumn.Type == CommandButtonType.Delete)
{
CountryId = args.RowData.CountryId;
//I want run this method to be executed if the confirm dialog has been clicked with ok button
Task.Run(() => DeleteCountry(CountryId));
}
}
public async Task DeleteCountry(long CountryId)
{
var country = countryList.First(c => c.CountryId == CountryId);
var result = await http.DeleteAsync(apiPath + "/" + CountryId);
string Message = "Record Successfully Deleted !!!";
if (result.StatusCode == HttpStatusCode.NotFound)
{
Message = "No such Country found to Delete Country";
}
else if (result.StatusCode == HttpStatusCode.BadRequest)
{
Message = result.ReasonPhrase;
}
else if (result.StatusCode == HttpStatusCode.OK)
{
await LoadCountryList();
}
}
public void AddNew()
{
navigationManager.NavigateTo("/AddCountry");
}
}
Here is my _Host.cshtml
@page "/"
@namespace eBizRiseServer.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>eBizRiseServer</title>
<base rel='nofollow' rel='nofollow' href="~/" />
<link rel="stylesheet" rel='nofollow' rel='nofollow' href="css/bootstrap/bootstrap.min.css" />
<link rel='nofollow' rel='nofollow' href="css/site.css" rel="stylesheet" />
<link rel='nofollow' rel='nofollow' href="_content/Syncfusion.Blazor/styles/material.css" rel="stylesheet" />
<link rel='nofollow'
rel='nofollow' href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.19.2/sweetalert2.min.css"
rel="stylesheet" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="js/Helpers.js"></script>
<script src="_framework/blazor.server.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.19.2/sweetalert2.min.js"></script>
</body>
</html>
The Delete Country method executes each time even though the Cancel button is clicked of dialog box.....
Please help in this