Raj K
Raj K

Reputation: 638

Converting a Model to a JSON object in ASP.NET Core MVC

How do I convert a Model to a JSON object in ASP.NET Core MVC?

I used to use the Json.Encode() method in MVC 5, but I cannot get this to compile in ASP.NET Core MVC. What is the replacement?

Upvotes: 1

Views: 2173

Answers (1)

Raj K
Raj K

Reputation: 638

Upto MVC 5, Json.Encode() works. But in MVC Core, we don't have Encode() method. Instead of it, we have Serialize() method. You can convert Model/ViewBag to json object as follows,

var x = @Html.Raw(Json.Serialize(Model));

Upvotes: 1

Related Questions