How to get the model data in javascript ?

Alick Wang 266 Reputation points
2023-05-08T03:27:54.5866667+00:00

How to get the model data in javascript ?

I use asp.net core 7 mvc.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,167 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,031 Reputation points
    2023-05-08T15:08:57.61+00:00

    there are two common approaches.

    the first is to render the model as a javascript object. that is render javascript code. fortunately json is valid javascript you you render the model data as json and assign to object

    <script>
    var model = @Html.Raw(JsonSerializer.Serialize(Model));
    </script>
    

    the second way for the javascript to make an ajax call.

    0 comments No comments

0 additional answers

Sort by: Most helpful