Converting C# objects to a javascript object in ASP.NET MVC Razor.

Yasser Shaikh
Apr 28, 2015 · 1 min read

There are times, when you need to pass a C# object as is to your JavaScript code, this is something that I do a lot of time and still keep forgetting.

Finally putting it down here.

<script type="text/javascript">
var projectJson = '@(Html.Raw(new JavaScriptSerializer().Serialize(Model)))';
window.project = JSON.parse(projectJson);
</script>

Yasser Shaikh's blog

It's supposed to be automatic, but actually you have to push this button.

Yasser Shaikh

Written by

Yasser Shaikh's blog

It's supposed to be automatic, but actually you have to push this button.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade