how to pass a razor value to a jquery function, in an mvc view

后端 未结 6 1357
耶瑟儿~
耶瑟儿~ 2020-12-09 02:31

So I\'m trying to pass a parameter to a javascript function using the razor \'@\' notation (in an MVC-4 View) but I\'m getting Syntax error: \"Unterminated string constant\"

相关标签:
6条回答
  • 2020-12-09 02:37

    As alternative, you may use the other way to get @item.ID in your jQuery function. Just add hiden span with it and get it's value in your function.

    Say, you have list:

    <ul>
    @foreach (var item in Model)
    {
     ...
       <li> <input type="button" value="Assign" onclick="AssignButtonClicked()" />
            <span class="hidenID">@item.ID</span>
    </li>
    }
    </ul>
    

    than in your script add:

    <script>
    $(document).ready(function(){
    $('.hidenID').hide() //**hide your span
    });
    
    function AssignButtonClicked()
    {
    ...
    var id = $(this).closest('li').find('.hidenID').text();
    ...
    }
    
    </script>
    

    But to my mind better approach for jQuery is as follows:

        <ul>
            @foreach (var item in Model)
            {
             ...
               <li> <input type="button" value="Assign" class="button" />
                    <span class="hidenID">@item.ID</span>
            </li>
            }
            </ul>
        <script>
            $(document).ready(function(){
            $('.hidenID').hide() //**hide your span
            });
    
            $(function(){
             $('.button').click(function(){
            ...
             var id = $(this).closest('li').find('.hidenID').text();
            ...
            }
        });
    
    });
    
        </script>
    
    0 讨论(0)
  • 2020-12-09 02:38

    To avoid this kind of issue, you may build all your js function call in c# code section.

    Instead of:

    <input type="button" value="Assign" onclick="AssignButtonClicked('@item.ID')" />
    

    Use:

    @{
      var assignFunctionCall = "AssignButtonClicked(" + item.ID + ")";
    }
    <input type="button" value="Assign" onclick="@assignFunctionCall " />
    
    0 讨论(0)
  • 2020-12-09 02:40

    Try this,

     <input type="button" value="Assign" onclick="AssignButtonClicked(@item.ID);" />
    

    Script

    <script type="text/javascript">
    
     function AssignButtonClicked(obj) {
            alert(obj);
            return false;
        }
    </script>
    
    0 讨论(0)
  • 2020-12-09 02:45

    You tried with:

    <input type="button" value="Assign" onclick="AssignButtonClicked('@item.ID');" />
    

    I added a semicolon at the end of javascript function call AssignButtonClicked('@item.ID');

    0 讨论(0)
  • 2020-12-09 02:52

    If you can use JQuery .data()

    <input type="button" value="Assign" onclick="AssignButtonClicked(this)" 
           data-assigned-id="@item.ID" />
    

    Further it can be fetched using

    function AssignButtonClicked(elem){
         var id= $(elem).data('assigned-id');
    }
    
    0 讨论(0)
  • 2020-12-09 02:57

    try this

     <input type="button" value="Assign"
       onclick="@("AssignButtonClicked('"+item.ID+"')")"  />
    
    0 讨论(0)
提交回复
热议问题