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.

1

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.