使用字符串

时间:2016-08-10 14:16:59

标签: javascript if-statement logic

所以非常奇怪,下面是警报值:

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

然而奇怪的是,combineRole上的警报给了我consumerSupport / ConsumerContact / GetEmailLog。所以这是因为一些不正当的理由进入if块。我期待它 转到else块,警报应该吐出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" })';
}

2 个答案:

答案 0 :(得分:4)

删除引号,你不是JS中的布尔值,而是字符串

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 :(得分:3)

您将consumerSupportEditableRolesuperAdminRole设置为字符串,而不是布尔值。所以combinedRole实际上是:

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

使其成为"false",即truthy