Question background:
I am attemting to check if a user is logged in to my site when a view is rendered.
The issue:
Currently I set the users username to a ViewBag property in a Controller then access this in its respective View. The issue I'm facing is the IF statement in my Razor syntax dosen't seem to be checking the ViewBag.userName property and in-turn is not then calling the JavaScript method to set the Inner HTML of a button with the users name.
As a side note I have - as a test - set the ViewBag property to a h3 tag just to make sure the users name is being passed to the view which it is.
The code:
My controller where the username is set:
ViewBag.userName = Session["userName"];
The View:
Check the status of the ViewBag property userName , if it not null i.e a users has logged in, then Call the ShowUsersName method to set the value to a buttons inner HTML.
@if (ViewBag.userName != null)
{
<script>
var userName= @ViewBag.userName;
ShowUsersName(userName);
</script>
}
ShowUsersName method:
<script type="text/javascript">
var ShowUsersName = function (userName) {
var html = '<span class="glyphicon glyphicon-user"></span> Hi, '+ userName +'"<strong class="caret glyphicon glyphicon-triangle-bottom"></strong>';
document.getElementById("userButton").innerHTML = html;
};
</script>