Access a Model property in a javascript file?

后端 未结 7 1426
醉梦人生
醉梦人生 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:08

    I had the same problem and I did this:

    View.

    `var model = @Html.Raw(Json.Encode(Model.myModel));
     myFunction(model);`
    

    External js.

    `function myFunction(model){
       //do stuff
     }`
    

提交回复
热议问题