1

Here in my ASP .NET Core MVC project I call JavaScript function with a parameter.

 <span onclick="workloadFunction(11)">Click</span>

Then in the function I wants to loop my model data and check a condition with if statement. So if statement write with c# but I cannot access the JavaScript parameter value inside the if statement.

<script>
function workloadFunction(idvalue) {
   @foreach(var project in Model.activeProjects)
   {
      @if(project.id == idvalue)
      {
          //rest of code
      }
   }
</script>

I wants to access 'idvalue' inside the if statement.

  • [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429) – VLAZ Jan 10 '22 at 07:10

1 Answers1

4
 <script>
    function workloadFunction(idvalue) {
     var items = @Html.Raw(Json.Encode(Model.activeProjects));
      for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.id == idvalue) {
             //rest of code
           }
         }
      }
 </script>
Amit
  • 823
  • 7
  • 13