imba22
imba22

Reputation: 649

issue with the simple if condition in javascript with string

So something very strange,below are the alert values:

However the strange thing is that the the alert on the combinedRole give me consumerSupport/ConsumerContact/GetEmailLog. So it is for some ungodly reason going in the if block. I would expect it to go to the else block and the alert should spit Implementation/Organization/GetEmailLog

var consumerSupportEditableRole = '@(Roles.IsUserInRole("Consumer Support Editable") ? "true" : "false")';
var superAdminRole = '@(Roles.IsUserInRole("Super Admin") ? "false" : "true")';
var combinedRole = consumerSupportEditableRole && superAdminRole
alert("consumerSupportEditableRole: " + consumerSupportEditableRole);
alert("superAdminRole: " + superAdminRole);
alert("superAdminRole && consumerSupportEditableRole: " + combinedRole);
if (combinedRole)
{
    var url = '@Url.Action("GetEmailLog", "ConsumerContact", new { Area = "ConsumerSupport" })';
}
else
{
    var url = '@Url.Action("GetEmailLog", "Organization", new { Area = "Implementation" })';
}

Upvotes: -1

Views: 53

Answers (2)

James Thorpe
James Thorpe

Reputation: 32202

You're setting consumerSupportEditableRole and superAdminRole to strings, not booleans. So combinedRole is actually:

var combinedRole = "true" && "false";

which makes it "false", which is truthy.

Upvotes: 3

Maxx
Maxx

Reputation: 1748

Remove quotes, you get not booleans in JS, but strings

var consumerSupportEditableRole = @(Roles.IsUserInRole("Consumer Support Editable") ? "true" : "false");
var superAdminRole = @(Roles.IsUserInRole("Super Admin") ? "false" : "true");
var combinedRole = consumerSupportEditableRole && superAdminRole
alert("consumerSupportEditableRole: " + consumerSupportEditableRole);
alert("superAdminRole: " + superAdminRole);
alert("superAdminRole && consumerSupportEditableRole: " + combinedRole);
if (combinedRole)
{
    var url = '@Url.Action("GetEmailLog", "ConsumerContact", new { Area = "ConsumerSupport" })';
}
else
{
    var url = '@Url.Action("GetEmailLog", "Organization", new { Area = "Implementation" })';
}

Upvotes: 4

Related Questions