Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to call a JavaScript function in c# code in asp.net mvc3 view, but don't know how to do this. My code is following

Javascript Function

function JK(){

   alert("Javascript Function Called From foreach");

  }

C# Foreach

foreach(var item in collection){ //I want to call JavaScript function here on every iterate.  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
284 views
Welcome To Ask or Share your Answers For Others

1 Answer

Well you can use something like this:

foreach (var item in collection) {
   <script type="text/javascript">
     JK();
   </script>
}

If you need to use foreach inside the javascript code, you should just use . Like this:

<script type="text/javascript">
   @foreach (var item in collection) {
      <text>JK();</text>
   }
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...