I have an ASP.NET MVC app. One of the views in my app has some JavaScript. I want to set the value of a JavaScript variable based on a value in the ViewBag. If the ViewBag value exists and is not null, I want to use it. Otherwise, I want to assign an empty string value to the JavaScript variable. Here's what I'm currently trying:
<script type='text/javascript'>
function formatItem(item) {
var itemName = @(ViewBag.ItemName ? ViewBag.ItemName : '');
alert(itemName);
}
</script>
This block throws an error that says:
Compiler Error Message: CS1011: Empty character literal
Is there a way to do what I'm trying to do? If so, how?
Thanks!
@Html.Raw()