有时我必须从 Mvc View 内部检查 ViewBag 内是否存在成员,以查看操作是否忘记分配该成员是否有任何问题。在我的 Razor View 中,我有:
@if(ViewBag.Utente.Ruolo.SysAdmin)
如何检查 ViewBag.Utente
是否已定义?
最佳答案
您必须检查所有对象是否为空。 Utente
、Utente.Ruolo
和 Utente.Ruolo.SysAdmin
可能为 null:
@if (ViewBag.Utente != null)
{
if (ViewBag.Utente.Ruolo != null)
{
if (!string.IsNullOrEmpty(ViewBag.Utente.Ruolo.SysAdmin))
{
//ViewBag.Utente.Ruolo.SysAdmin has value..you can use it
}
}
}
关于asp.net-mvc - 检查 Mvc ViewBag 中的成员是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17972685/