Not understanding why this is not deleting the record.
Task Delete gets the NameId
[HttpDelete("{id}")] private async Task DeleteUserName(int NameId)
{
var userNameSelected = usernames.First(x => x.NameId == NameId);
if (await js.InvokeAsync<bool>("confirm", $"want to delete {userNameSelected.Email}?"))
{
var httpClient = ClientFactory.CreateClient("ServerAPI");
await httpClient.DeleteAsync($"api/usernames/{NameId}");
StateHasChanged(); or navManager.NavigateTo("usernames");
}
}
public async Task<ActionResult> Delete(int id)
{
var userName = new UserName { NameId = id };
if (userName == null)
{
return NotFound();
}
_context.Remove(userName);
await _context.SaveChangesAsync();
return NoContent();
}
This all works and NoContent() is returned from the controller, however the page does not get updated and I need a CTRL F5 to refresh the page regardless if I use StateHasChanged() or NavigateTo()
What is the magic here to refresh the page?
Click on "Mark as Answer" if my answers help you along.
Seems you are just doing an http request and then nothing at all. The web page won't refresh automagically. For example if the info is found in an html table row, then once you know the Ajax request deleted the user on the server side, you have to remove
this row from the html table to "repaint" the page without fetching the whole page again.
Contributor
2619 Points
2753 Posts
HttpDelete - needs CTRL F5 to update page
Jun 05, 2020 05:42 PM|wavemaster|LINK
Not understanding why this is not deleting the record.
Task Delete gets the NameId
This all works and NoContent() is returned from the controller, however the page does not get updated and I need a CTRL F5 to refresh the page regardless if I use StateHasChanged() or NavigateTo()
What is the magic here to refresh the page?
All-Star
53721 Points
24048 Posts
Re: HttpDelete - needs CTRL F5 to update page
Jun 05, 2020 06:06 PM|mgebhard|LINK
The code is a little confusing. Are you trying to do an AJAX request to DeleteUserName?
All-Star
48730 Points
18189 Posts
Re: HttpDelete - needs CTRL F5 to update page
Jun 05, 2020 07:23 PM|PatriceSc|LINK
Hi,
Seems you are just doing an http request and then nothing at all. The web page won't refresh automagically. For example if the info is found in an html table row, then once you know the Ajax request deleted the user on the server side, you have to remove this row from the html table to "repaint" the page without fetching the whole page again.
Contributor
2619 Points
2753 Posts
Re: HttpDelete - needs CTRL F5 to update page
Jun 05, 2020 07:30 PM|wavemaster|LINK
I understand that is what is going on, however I had gotten the impression that one of these would do the trick:
But they don't.
I know it hits the controller and removes the record.
How to refresh the page, Blazor style?
Contributor
2619 Points
2753 Posts
Re: HttpDelete - needs CTRL F5 to update page
Jun 06, 2020 11:56 AM|wavemaster|LINK
Ended up finding something where people just reloaded the whole list. That works.
Contributor
2619 Points
2753 Posts
Re: HttpDelete - needs CTRL F5 to update page
Jun 06, 2020 11:59 AM|wavemaster|LINK
No, it is just a call to the Controller.
That JS you see there is to put up a confirm dialog before actually deleting the record.
Still working with that Students thingy from earlier this week, with some mods to make it useful for me.