Asp.net - 对象引用未设置为对象的实例 [英] Asp.net - object reference not set to an instance of an object

查看:119
本文介绍了Asp.net - 对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看用户可以查看他们所做预订的视图。我基本上有一个模型,其中包含两种不同类型的预订清单,包括大厅和房间。因此,该模型包含大厅预订列表和房间预订列表。在我的控制器中,我正在尝试将所有大厅预订添加到大厅预订列表中,并且同样用于预订房间。但我收到此错误



I'm trying to make a view where a user can see bookings they have made. I basically have a model that holds a list of 2 different kinds of bookings in different facilities, a hall and a room. So the model holds a list of hall bookings and a list of room bookings. In my controller, i'm trying to add all the hall bookings to the hall bookings list, and the same for the room bookings. But I am getting this error

Quote:

对象引用未设置为一个对象的实例。

"Object reference not set to an instance of an object."





这是我的模特:





Here is my model:

public class AllBookingsViewModel
{

    public List<HallActivityBooking> HallBookingsList { get; set; }

    public List<RoomBooking> RoomBookingsList { get; set; }

}





这是我的控制器:





Here is my controller:

public class BookingsController : BaseController
{
    // GET: Bookings
    public ActionResult Bookings()
    {

        //Get current user
        var userId = User.Identity.GetUserId();
        var currentUser = db.Users.Find(userId);

        AllBookingsViewModel model = new AllBookingsViewModel();

        //Fill activity booking
        foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id))
        {
            model.HallBookingsList.Add(hallBooking);
        }


        //Fill room bookings list
        foreach (RoomBooking roomBooking in db.RoomBookings.Where(x => x.CustomerUser.Id == currentUser.Id))
        {
            model.RoomBookingsList.Add(roomBooking);
        }

        return View(model);
    }







帮助将被挪用。



我尝试了什么:



我试过宣布模型如下:






Help would be appropriated.

What I have tried:

I've tried declaring the model like:

var model = new AllBookingsViewModel





我尝试将FirstOrDefault添加到linq语句中:





I've tried adding "FirstOrDefault" to the linq statement:

foreach (HallActivityBooking hallBooking in db.HallBookings.Where(x => x.CustomerUser.Id == currentUser.Id).FirstOrDefault())
{
    model.HallBookingsList.Add(hallBooking);
}





这给了我这个错误:





This gives me this error:

Quote:

严重级代码描述项目文件行抑制状态

错误CS0446 Foreach无法运行''方法组'。你打算调用'方法组'吗? ... 26活动

Severity Code Description Project File Line Suppression State
Error CS0446 Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'? ... 26 Active

推荐答案

你为什么要做 foreach 一个只能返回null或一个对象的查询(FirstOrDefault)?



你得到NullReferenceException,因为你的代码试图调用方法或获取/设置对象的属性为null。您没有说哪行代码会引发错误但是因为您有一个 foreach 尝试枚举只能返回单个对象的查询或null,我'我将假设该行正在抛出。



FirstOrDefault将返回集合中的第一个对象,如果集合为空或null,则返回默认值为了表达。 对于始终为null的对象类型。对于值类型,通常为0.



由于您的查询表达式显然与Where子句中的任何内容都不匹配,因此您尝试执行 foreach on FirstOrDefault返回的null。
Why are you doing a foreach on a query that can only ever return null or a single object (that FirstOrDefault)?

You get the NullReferenceException because you're code is trying to call a method or get/set a property on an object that is null. You don't say which line of code throws the error but since you've got a foreach trying to enumerate a query that can only return a single object or null, I'm going to assume that line is throwing.

FirstOrDefault will return the first object in a collection or, if the collection is empty or null, it will return the default value for the expression. For object types that's always null. For values types, usually 0.

Since your query expression apparently didn't match anything in the Where clause, you're trying to do a foreach on the null returned by FirstOrDefault.


这篇关于Asp.net - 对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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