MVC - pass ViewData as boolean

Sorry for a newbie question.

When passing boolean value from controller to the view using ViewData, how do I retrieve it as a boolean value in javascript? example:

Controller:

ViewData["login"] = true;

View

    <script type="text/javascript">
var login = <%= (bool)ViewData["Login"] %>;   /// this doesn't work, throw javascript error;
</script>

yeh surely i can do

  <script type="text/javascript">
var login = '<%= ViewData["Login"] %>';   /// now login is a string 'True'
</script>

But i rather keep login object as a boolean rather a string if that's possible

Thanks!

Answers


Just remove the single quotes.

<script type="text/javascript">
    var login = <%= (bool)ViewData["Login"] ? "true" : "false" %>;
</script>

This will result in:

var login = true;

Which will be parsed as a boolean in the browser.


Need Your Help

Call method every time a controller method is ending

c# asp.net-mvc asp.net-mvc-4 razor

Basically I got a static class called NotificationManager. Every time a controller method is called I want to store the modelerrors in the notificationmanager.

The multi-part identifier “Maintenanceinfo.userid” could not be found

asp.net sql sql-server vb.net

I am trying to display details in a gridview from two tables and the login is tree structure, Admin, Manager, User