How to get the model data in javascript ?

Alick Wang 306 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.

Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.