Search code examples
javascriptjqueryrazorstring-literals

Passing string to javascript function in razor


I am passing 3 parameters to a javascript function from razor code, the first two are passed properly but the third one is never passed - the first two are int/numbers but the last one is a string

THIS WORKS FINE

if (geResult.ResultValue != null) { 
    var val = "A!";
    <script>{ getPosition(@geResult.assessmentId, @index, "A!"); }</script>
}

THIS DOESNOT WORK

if (geResult.ResultValue != null) { 
    var val = "A!";
    <script>{ getPosition(@geResult.assessmentId, @index, @val); }</script>
}

Solution

  • Try this--Just add single quotes

    if (geResult.ResultValue != null) { 
    <script>{ getPosition('@geResult.assessmentId', '@index', '@val'); }</script>
    }
    

    Hope it Works..