tags:

views:

353

answers:

2

Hello,

I would like to access a Model property value in ASP MVC at the View's javascript section

What is the best way to do so?

Thanks

+4  A: 

I can't think of another way then just simpy printing it ;

<script type="text/javascript">
    var myJavaScripVar = '<%=Model.Property %>';
</script>
çağdaş
Agreed, just remember to escape the property in case it contains quotes etc...
jasondoucette
Great!!! thank you
Ws
I'd also consider writing an extension method just to help clean things up a bit on your view, something that would look like <%=Html.JSVarFor(x=>x.Property, "myJavaScriptVar")%>
Chao
A: 

Another option is to use an Ajax request to a controller method that returns a json result.

Matthew