可以为null的对象必须具有mvc中的值

时间:2018-02-07 10:40:51

标签: c# .net asp.net-mvc

当我运行网站时,我收到此错误:

  

System.InvalidOperationException:'Nullable对象必须有值。'

我有async function myFunc() { const data = await request('http://......./', query); const allData = data; } 控制器,其中包含以下代码:

Duzenle

此行引发异常:

[Authorize(Roles = "Sube")]
public ActionResult Duzenle()
{
    var date = DateTime.Now.Date;
    var kullanici = UserGetter.GetUser();
    var subeId = kullanici.SubeId.Value; // exception is thrown here
    var kasaRaporu = db.KasaRaporu
        .Where(kr => kr.SubeId == subeId).ToList()
        .Where(kr => kr.Tarih.Date == date).FirstOrDefault();

    if (kasaRaporu != null)
    {
        return View(kasaRaporu);
    }

    kasaRaporu = KasaRaporuCreator.Create(subeId);
    return View(kasaRaporu);
}

有什么问题?

1 个答案:

答案 0 :(得分:1)

显然kullanici.SubeIdnull。当您尝试访问Value可空的null时,您将收到此异常。检查GetUser方法,确保SubeId在返回时不是null