-1

So something very strange,below are the alert values:

  • consumerSupportEditableRole = true
  • superAdminRole = false
  • combinedRole = false

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" })';
}
1
  • Don't use strings to hold boolean values, and you will be sweet. Commented Aug 10, 2016 at 14:20

2 Answers 2

4

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" })';
}
Sign up to request clarification or add additional context in comments.

3 Comments

'true' is not the same as true
@rpadovani - he did, as that is serverside code, no longer outputted inside quotes, hence removed.
@rpadovani you don't need to. Seems like it's C# generates JS code, in C# "true" and "false" is string i guess, but qoutes will not prints, so in JS it becomes boolean.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.