Reputation: 356
I have a variable locally defined in my View. I am setting it to 1 under some conditions. I want to pass this variable to the script in order to have a condition based on it that decides if an audio is played or not.
How can I pass alarm variable to my javascript?
@{
int alarm = 0;
}
@foreach (var item in Model)
{
if (ViewBag.Dtype == "1")
{
alarm = 1;
}
}
<script type="text/javascript">
$(document).ready(function () {
document.getElementById('player').play()});
</script>
Upvotes: 0
Views: 161
Reputation: 3023
You can store the Razor variable into javascript variable.
Like below, storing alarm
into javascript variable jsAlarm
. Now you can use jsAlarm
for your condition check.
@{
int alarm = 0;
}
@foreach (var item in Model)
{
if (ViewBag.Dtype == "1")
{
alarm = 1;
}
}
<script type="text/javascript">
var jsAlarm=@alarm;
$(document).ready(function () {
document.getElementById('player').play()});
</script>
Upvotes: 1