Access a Model property in a javascript file?

后端 未结 7 1424
醉梦人生
醉梦人生 2020-12-17 14:22

Is it possible to access a Model property in an external Javascript file?

e.g. In \"somescript.js\" file

var currency = \'@Model.Currency\';
alert(cu         


        
7条回答
  •  [愿得一人]
    2020-12-17 15:09

    Try JavaScriptModel ( http://jsm.codeplex.com ):

    Just add the following code to your controller action:

    this.AddJavaScriptVariable("Currency", Currency);
    

    Now you can access the variable "Currency" in JavaScript.

    If this variable should be available on the hole site, put it in a filter. An example how to use JavaScriptModel from a filter can be found in the documentation.

提交回复
热议问题