The js file cannot directly access C # objects, so you need to do something like below. Even if you want to write the full jQuery code in a view file, you can still follow the same approach.
That way, you can pass the variables to some model that is passed to the View, and when you have these variables in the Model, you can do something like below:
<script type="text/javascript"> var myList= @Html.Raw(Json.Encode(@Model.UsersList)); </script>
So now you have a json object that can be accessed from any single js file from the same view file using the variable "myList".
source share