How can I call a javascript function in the view of my ASP MVC application

I have the following JavaScript function on my browse page:

<script type="text/javascript">
  function func(nam) {
    alert(nam);
</script>

My view code for calling this function is similar to this page:

@foreach (var item in Model)
{
  <script>func(@item.name) </script>
}

It does not give any result.

I need to call a JavaScript function from html, but this does not happen. Please help me with this.

Is there any other way to call a JavaScript function?

+4
source share
1 answer

Assuming your property item.Namehas a string value SomeThing. So when the razor displays your page, your generated markup will be

<script> func(SomeThing) </script>

javascript , SomeThing js. js , . , script,

SomeThing

. .

<script>func('@item.name') </script>

, func javascript , .

<script>    
    function func(nam) {
        alert(nam);
    }
</script>

<script>func('@item.Name') </script>

js , / .

<script src="~/Scripts/YourJsFileWherFuncIsDefined.js"></script>
<script>func('@item.Name') </script>
+5

Source: https://habr.com/ru/post/1626408/


All Articles