navigationManager.NavigateTo not work after delete the record

Viewed 494

navigate.manager not work after delete the record

I am trying to when record is deleted then move on this page(https://localhost:44399/DisplayEmployeeData)

my record is deleted successfully but note navigate and give the error

enter image description here

Client Side:

DeleteEmployee.razor

@using CrudBlazorServerApp.Data
@page "/DeleteEmployee/{id}"
@inject HttpClient Http
@using System.Net.Http
@inject NavigationManager navigationManager

<h3>DeleteEmployee</h3>

<div class="row">
    <div class="col-md-4">

        @if (emp != null)
        {
            <form>
                <div class="form-group">
                    <label for="username" class="control-label">username:</label>
                    @emp.username
                </div>
                <div class="form-group">
                    <label asp-for="empaddress" class="control-label">empaddress:</label>
                    @emp.empaddress
                </div>
                <div class=" form-group">
                    <label asp-for="password" class="control-label">password:</label>
                    @emp.password
                </div>
                <div class=" form-group">
                    <label asp-for="country" class="control-label">country:</label>
                    @emp.country
                </div>
                <div class="form-group">
                    <button @onclick="Delete">Delete</button>
                </div>
            </form>
        }
    </div>
</div>

@code {

    [Parameter]
    public string Id { get; set; }

    Emp emp;
    protected override async Task OnInitializedAsync() =>
        emp = await Http.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/getemp/" + Id);

    private async Task Delete()
    {
        @if (Id != null)
        {
            
            string url = "https://localhost:44333/api/emps/checkDelete?empid=" + Id;
            await Http.GetFromJsonAsync<Emp>(url);
            navigationManager.NavigateTo("https://localhost:44399/DisplayEmployeeData"); //here I am facing issue not navigate
        }
    }
}


ServerSide(WebApi):

EmpsController.cs

        [HttpGet]
        public Emp checkDelete(int empid, string username, string empaddress, string password, string country)
        {
            var deletercord = _context.emps
                .Where(s => s.empid == empid)
                .FirstOrDefault();
            deletercord.username = username;
            deletercord.empaddress = empaddress;
            deletercord.password = password;
            deletercord.country = country;

            _context.emps.Remove(deletercord);
            _context.SaveChanges();

            return deletercord;
        }

I want to when user press the delete then navigate the page

1 Answers

Your page gets a 404 not found error at the line emp = await Http.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/getemp/" + Id); It is not the navigationManager. Change your OnInitializedAsync() method with a try-catch block into something like this:

public override async void OnInitialized()
    {
        try 
        {
            emp = await Http.GetFromJsonAsync<Emp>("https://localhost:44333/api/emps/getemp/" + Id);
        }
        catch(Exception exc)
        {
            Console.WriteLine("Employee deleted ");
emp = new Employee() {Id=0};
        }
}
Related