简体   繁体   English

Blazor WASM 应用程序在处理执行删除请求的模态 ok 事件回调后停止响应

[英]Blazor WASM application stops responding after handling modal ok eventcallback, which performs a delete request

I implemented a delete button for a table, and when clicking the button a modal is shown to make sure the user really wants to delete that entry, but after the ok button is clicked, the application stops responding.我为表格实现了一个删除按钮,当单击该按钮时,会显示一个模式以确保用户确实想要删除该条目,但在单击确定按钮后,应用程序停止响应。 This only happens if I both delete and then get the data from the database using http request to my ASP.NET REST API.只有当我使用 http 对我的 ASP.NET REST API 的请求从数据库中删除并获取数据时才会发生这种情况。

Here are the relevant codes:以下是相关代码:

 @code{
    private async Task HandleCinemaModalOk(MouseEventArgs e)
    {
        visibleCinemaModal = false;
        var deleteResponse = await httpClient.DeleteAsync($"list_movies/{deleteableMovieId}");
        movies = await MoviesClient.GetMovies();
    }
 }

MoviesClient:电影客户端:

public async Task<IEnumerable<Movie>> GetMovies() =>
            await httpClient.GetFromJsonAsync<IEnumerable<Movie>>("list_movies");
       

HTML part: HTML部分:

@*Extra column for edit and delete*@
    <AntDesign.Column Width="300" @bind-Field="@context.Id" Title="">
        @{
            deleteableMovieId = @context.Id;
        }
        <Button type="danger" @onclick="@(() => visibleCinemaModal = true )" icon="delete">
        </Button>
        <Modal Title="Delete Movie"
               Visible="visibleCinemaModal"
               OnOk="HandleCinemaModalOk"
               OnCancel="HandleCinemaModalCancel">
            <Text>Are you sure you want to delete @context.Title ?</Text>
        </Modal>

        <Button type="primary" @onclick="@(() => NavigationManager.NavigateTo(@$"/editmovie/{context.Id}" ))" icon="edit">
        </Button>

    </AntDesign.Column>

Controller: Controller:

        [HttpGet]
        public async Task<ActionResult<List<Movie>>> GetMovies()
        {
            return await _db.Movies.ToListAsync();
        }


        [HttpDelete("{id}")]
        [Route("list_movies/{id}")]
        public async Task<ActionResult<int>> DeleteMovieById(int id)
        {
            var movie = await _db.Movies.FirstOrDefaultAsync(i => i.Id == id);
            if (movie == null)
            {
                return NotFound();
            }
            _db.Movies.Remove(movie);
            await _db.SaveChangesAsync();
            return Ok(movie);
        }

So if I don't add the line movies = await MoviesClient.GetMovies();因此,如果我不添加行movies = await MoviesClient.GetMovies(); the application doesn't freeze, but it's not an option, because the UI must update to show the user that the deletion was successful.应用程序不会冻结,但这不是一个选项,因为 UI 必须更新以向用户显示删除成功。

After a long wait, I get the following error in the console:经过漫长的等待,我在控制台中收到以下错误:

 fail: Microsoft.WebAssembly.Diagnostics.DevToolsProxy[0] DevToolsProxy::Run: Exception System.AggregateException: One or more

errors occurred.发生错误。 (The remote party closed the WebSocket connection without completing the close handshake.) ---> System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. (对方没有完成关闭握手就关闭了WebSocket连接。) ---> System.Net.WebSockets.WebSocketException (0x80004005): 对方没有完成关闭握手就关闭了WebSocket连接。 at System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(Boolean throwOnPrematureClosure) at System.Net.WebSockets.ManagedWebSocket.EnsureBufferContainsAsync(Int32 minimumRequiredBytes, CancellationToken cancellationToken, Boolean throwOnPrematureClosure) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder 1.StateMachineBox 1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](Memory 1 payloadBuffer, CancellationToken cancellationToken) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder 1.StateMachineBox 1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token) at System.Threading.Tasks.ValueTask 1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) --- End of stack trace from previous location --- at Microsoft.WebAssembly.Diagnostics.DevToolsProxy.ReadOne(WebSocket socket, CancellationToken token) --- End of inner except在 System.Net.WebSockets.ManagedWebSocket.ThrowIfEOFUnexpected(布尔值1.StateMachineBox ) .Tasks.Sources.IValueTaskSource.GetResult(Int16 令牌)在 System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TResult](内存1 payloadBuffer, CancellationToken cancellationToken) at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder 1.StateMachineBox 1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token) at System.Threading.Tasks.ValueTask 1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state) --- 上一个位置的堆栈跟踪结束 - -- 在 Microsoft.WebAssembly.Diagnostics.DevToolsProxy.ReadOne(WebSocket socket, CancellationToken token) --- 内部 except 结束ion stack trace --- at Microsoft.WebAssembly.Diagnostics.DevToolsProxy.Run(Uri browserUri, WebSocket ideSocket)离子堆栈跟踪 --- 在 Microsoft.WebAssembly.Diagnostics.DevToolsProxy.Run(Uri browserUri,WebSocket ideSocket)

Edit:编辑:

It turned out that the problem was that I've put the modal inside a column, and when I clicked on the delete icon, it opened a modal for each record, but the ok button closed the "wrong" modal, and the EventCallback couldn't return.原来问题出在我把模态放在一个列里面,当我点击删除图标时,它为每条记录打开了一个模态,但是 ok 按钮关闭了“错误”的模态,EventCallback 不能'返回。

[HttpGet]
    public async Task<ActionResult<List<Movie>>> GetMovies()
    {
        var l= await _db.Movies.ToListAsync();
        return Ok(l);

    }

you're not returning an ActionResult, so I bet it's getting caught up doing a database call, try changing to code above.您没有返回 ActionResult,所以我敢打赌它在进行数据库调用时遇到了麻烦,请尝试更改为上面的代码。

Similarly, from your delete action you're returning ActionResult not ActionResult.同样,从您的删除操作中,您返回的是 ActionResult 而不是 ActionResult。 Change that to return Ok(0) (if you client is expecting an int, or change the signiture to ActionResult).将其更改为返回 Ok(0)(如果您的客户需要一个 int,或将签名更改为 ActionResult)。

Finally, I suspect you don't want to be awaiting the db save changes call, you actually want to wait for it.最后,我怀疑您不想等待 db save changes 调用,您实际上想要等待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM