Just ask your function to accept the argument and use it in the alert (or anywhere).
external.js
function myFunc(value) { alert(value); }
someview.cshtml
<script> myFunc(@myValueFromModel); </script>
One thing to keep in mind is that if myValueFromModel is a string, then it will pass as myFunc(hello) , so you need to wrap it in quotation marks so that it becomes myFunc('hello') , like this
myFunc('@(myValueFromModel)');
Note the optional () used with the razor. This helps the engine discern where the gap is between the razor code, so nothing strange happens. This can be useful when there are nested ones ( or " .
change
If this is done several times, then some changes may occur at the end of JavaScript. Mostly, the example shown does not display the script correctly. It will need to be changed. You can use a simple structure like this.
jsFiddle Demo
external.js
var myFunc= new function(){ var func = this, myFunc = function(){ alert(func.value); }; myFunc.set = function(value){ func.value = value; } return myFunc; };
someview.cshtml
<script> myFunc.set('@(myValueFromModel)'); myFunc();</script>
source share