Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How access JavaScript variable inside c# code

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

 <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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading