简体   繁体   中英

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. 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#?

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

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.

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.

Edit 1

Check Do some work after the response in ASP.NET Core to understand how OnCompleted is used (a use case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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