1

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?

MaxinhoHD
  • 49
  • 1
  • 6

1 Answers1

1

Check the function documentation here: https://learn.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).

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Thanks but it seems very complicated to me. Do you have an idea what I could do instead I'm new to C# and Razor. Or ist there another way to load the website and then later add data to it via c#? – MaxinhoHD Oct 06 '20 at 14:09
  • Make an ajax call from the clientside to a controller action: https://www.codeproject.com/Articles/41828/JQuery-AJAX-with-ASP-NET-MVC very nice guide here. – Athanasios Kataras Oct 06 '20 at 14:15
  • Ok but the problem is I can't use Controllers in my project, I also don't have the Global.asax file. I think I got a different kind of project (Razor). – MaxinhoHD Oct 06 '20 at 15:11