Reputation: 638
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
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