Calling JavaScript function in MVC 5 Razor view

前端 未结 2 468
遇见更好的自我
遇见更好的自我 2021-01-05 03:58

I have seen in another post that you can call a JavaScript function in your razor code like so:

@:FunctionName()

For me though this only ou

相关标签:
2条回答
  • 2021-01-05 04:19

    You need to put your javascript in a <script> tag, and you need to call the functions within their scope:

    <script type="text/javascript">
    
        $(document).ready(
            function ShowQuote() {
                $(".quote").show();
            },
            function ShowClarify() {
                $(".clarify").show();
            }
    
            @if (@Model.clarify == true)
            {
                // do drop down loic
                ShowClarify();
            }
            else
            {
                // fill quote
                ShowQuote();
            }
        );
    
    </script>
    
    0 讨论(0)
  • 2021-01-05 04:22

    If you are passing any parameter to the JavaScript function, it must be enclosed with quotes ('').

    foreach (var item in files)
        {
            <script type="text/javascript">
                Attachment(**'@item.FileName'**, **'@item.Size'**);
            </script>  
        } 
    
    0 讨论(0)
提交回复
热议问题