Can I write my razor code in javascript like this:
<script type="text/javascript">
$(document).ready(function () {
alert("test");
@if (Model != null)
{
foreach (var item in Model)
{
alert(item);
}
}
});
</script>
I'm getting a error that alert is not defined
<script type="text/javascript">
$(document).ready(function () {
alert("test");
@if (Model != null) {
foreach (var item in Model) {
@:alert(item);
}
}
});
</script>
Since you are inside the razor code block, you need to tell razor alert()
is not a part of razor code block via @:
Alternatively, you can use <text>
element.
<text>
alert(item);
</text>
foreach (var item in Model)
{
<text>
alert(item);
</text>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With