简体   繁体   English

如何从 razor onComplete 调用 JS 函数?

[英]How can I call a JS function from razor onComplete?

okay, so my problem is a little bit difficult.好的,所以我的问题有点困难。 I have a cshtml page that has a bit of code that is executed onCompleted and which then gets data from the Model.我有一个 cshtml 页面,其中有一些代码在 onCompleted 上执行,然后从模型中获取数据。 I want the result to be inserted into an table:我希望将结果插入到表中:

Response.OnCompleted(async () => {
   string table = await Model.getTable();

    //Set table as innerHtml from my table
});

So how can I either call a js function which then sets the innerHtml or can I set the innerHtml directly via C#?那么我如何调用一个 js 函数然后设置innerHtml,或者我可以直接通过C#设置innerHtml?

I have to run it like that because I want it to fill the data after the page is loaded.我必须像这样运行它,因为我希望它在页面加载后填充数据。 Is that even possible how I want it to be?这甚至有可能成为我想要的样子吗?

Check the function documentation here: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpresponse.oncompleted?view=aspnetcore-3.1在此处查看功能文档: https : //docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpresponse.oncompleted?view=aspnetcore-3.1

Adds a delegate to be invoked after the response has finished being sent to the client.添加要在响应完成发送到客户端后调用的委托。

This effectively means that the client (javascript) has not idea what happens there.这实际上意味着客户端 (javascript) 不知道那里发生了什么。

The code is run server side after the client side (where the javascript resides) has taken the response, so there is not way to run any javascript from the OnCompleted delegate.代码在客户端(javascript 所在的位置)接收响应后在服务器端运行,因此无法从OnCompleted委托运行任何 javascript。

Edit 1编辑 1

Check Do some work after the response in ASP.NET Core to understand how OnCompleted is used (a use case).检查在 ASP.NET Core 中的响应之后做一些工作以了解如何使用OnCompleted (一个用例)。

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

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