Let’s say I have following ASP.NET Web Form engine code, how can I express it in Razor engine?
<script type="text/javascript">
var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;
</script>
Thanks
Hardy
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would use the following:
This is exactly the same as your example (note the
Html.Raw).If you want the output (html)encoded or your code returns an IHtmlString :
You do want to use
@( ... )syntax, because using@new JavaScriptSerializer(..)will let the Razor parser stop at the first space (after new).The syntax like this:
does not work because it will call
new JavaScriptSerializer, but discard the output.