Possible Duplicate:
using razor within javascript
I would like to place a siimple value from the model on a razor page and use it as a constant value in a javascript function. i.e.
<script> var myValue = @Model.myRecord.Count();</script>
so that myValue = the record count in my model. I am using myRecord.Count as an example, it could be any value from my model.
Is this possible?
TIA J
OK I stumbled across the following solution:
<script> var myValue = @(Model.myRecord.Count())</script>
Just putting inthe extra brackets helped.
Make sure this is in a Razor file:
If it is in just a js file, the Razor engine won't run that code at all.
Sure, just make sure to properly encode it. For example you could JSON encode the entire model itself:
or:
or whatever.
But if you only care about the number of elements inside the model (if this model represents a collection):