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:


ViewData["login"] = true;


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

yeh surely i can do

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

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



Just remove the single quotes.

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

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# 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 sql sql-server

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

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.