【发布时间】:2016-09-24 05:44:48
【问题描述】:
我有一个类型化对象数组,它从控制器传递到我的视图中。要求是检查字符串值是否与该数组中的任何AdminEmailAddress 字符串类型对象匹配。
我尝试创建一个if condition 来检查字符串currentUserEmail 是否匹配该列表中的任何管理员电子邮件,使用indexOf。
但脚本在 if 条件下中断:
if (adminUserList.AdminEmailAddress.indexOf(currentUserEmail) > -1)
我还在currentUserEmail 字符串和adminUserList 数组上设置了警报,这两个数组都填充了数据。
运行时adminUserList的内容如下供参考:
var adminUserList = [{"AdminID":1,"AdminDisplayName":"brianb","AdminEmailAddress":"brian@test.com"},{"AdminID":2,"AdminDisplayName":"wendy","AdminEmailAddress":"wendy@test.com"}];
问题:
如何检查字符串是否与数组中的属性值匹配?
代码:
包含 AdminEmailAddress 属性的列表类型 -
List type public class AdminUsers
{
public int AdminID { get; set; }
public string AdminDisplayName { get; set; }
public string AdminEmailAddress { get; set; }
}
从Controller传入View的List:
List<AdminUsers> adminList = sqlConnection.GetAdminUsers();
ViewBag.AdminUserList = adminList;
包含if condition: 的脚本
<script>
var currentUserEmail = '@ViewBag.CurrUserEmail';
var adminUserList = @Html.Raw(Json.Encode(ViewBag.AdminUserList));
$(document).ready(function () {
var historyTable = $('#table').DataTable({
"order": [[6, "desc"]]
});
if (adminUserList.AdminEmailAddress.indexOf(currentUserEmail) > -1)
{
historyTable.columns([9]).visible(false);
}
});
</script>
【问题讨论】:
标签: javascript arrays model-view-controller types indexof