MVC 5中的应用程序用户列表是空的? [英] Applicationuser list in MVC 5 is empty?

查看:53
本文介绍了MVC 5中的应用程序用户列表是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前正在使用c#开发一个MVC 5项目。



我正在尝试创建一个ApplicationUsers列表来查找客户对应于正确的发票。我尝试了下面的代码,但得到了一个N​​ullReferenceException:



System.NullReferenceException:对象引用未设置为对象的实例。



控制器中的代码:



Hello, I'm currently working on an MVC 5 project with c#.

I'm trying to create a List of the ApplicationUsers to find the customer that corresponds to the correct Invoice. I tried the following below, but got a NullReferenceException:

System.NullReferenceException: Object reference not set to an instance of an object.

The code in my Controller:

private string FindCustomerForReservation(List<Invoice> invoices, List<ApplicationUser> customers, Reservation currentReservation) 
{ 
    string customername; Invoice correspInvoice = invoices.Find(f => f.Id == currentReservation.Invoicenumber); 
    ApplicationUser customer =  customers.Find(k => k.Id == correspInvoice.CustomerId);   
       
    customername = customer.UserName; return customername; 
}





客户似乎是空的,但我不知道为什么。



如何解决这个问题?



我的尝试:



我试图查看调试模式,但我仍然无法弄清楚为什么客户会空着。



"customer" seems to be empty but I don't know why.

How would I fix this?

What I have tried:

I've tried to look in debugging mode, but I still can't figure out why customer stays empty.

推荐答案

如果 customers 为null,这是因为您的视图代码未将任何内容传递回控制器以填写 customers 变量。



不可能告诉你为什么那是你发布的。
If customers is null, it's because your view code isn't passing anything back to the controller to fill in the customers variable.

It's impossible to tell you why that is from what you posted.


你只需要添加一个空检查



You just need to add a null check

if( customers != null && customers.Count > 0){
   //do the query here
}


这篇关于MVC 5中的应用程序用户列表是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆