1

My app may be bad I'm trying to learn What is the solution for this error? The error i got

Controller I do not know exactly how to make the list, the examples I tried did not work I guess I need to make a list somehow

     public IActionResult Rooms()
        {

            using (var db = new HotelWebContext())
            {
                var roomList = db.Rooms.Where(z => z.OtherTypeId == z.OtherType.OtherTypeId && z.RoomTypeId == z.RoomType.RoomTypeId && z.StatusId == z.Status.RoomStatusId).Select(r => new { r.RoomId, r.OtherType.OtherTypeName, r.OtherType.BasePrice, r.OtherType.Capacity, r.OtherType.BedType, r.OtherType.Services, r.RoomType.RoomTypeName, r.Status.RoomStatusName }).ToList();

                return View(roomList);
            }


        }

Model I created it automatically with the entity framework

public partial class Room
    {
        public Room()
        {
            Reservations = new HashSet<Reservation>();
        }

        public int RoomId { get; set; }
        public int? RoomTypeId { get; set; }
        public int? OtherTypeId { get; set; }
        public int? StatusId { get; set; }

        
        public virtual OtherType OtherType { get; set; }
        public virtual RoomType RoomType { get; set; }
        public virtual RoomStatus Status { get; set; }
        public virtual ICollection<Reservation> Reservations { get; set; }
    }

Rooms.cshtml have a foreach loop

@model IEnumerable<WebApplication3.Models.Room>

@{
    ViewData["Title"] = "Odalarımız";
    ViewData["Dark"] = "text-dark";
    ViewData["Info"] = "text-info";
}
<div class="odafiyatlari mx-auto">
    <div class="odaimg">
        <div class="container">
            <h1 style="margin-top: 200px; margin-bottom: 200px;font-family: 'Playfair Display', serif;" class="text-center text-dark ">ODALARIMIZ</h1>
            <div class="row">


                <!-- @for (int i = 0; i < 0; i++)
                {
                    <div class="col-xl-4 py-4">
                        <div style="border-radius: 10px;" class="card mx-auto" style="width: 18rem;">
                            <img class="card-img-top " src="~/images/oda1.jpg" alt="Card image cap">
                            <div class="card-body">
                                <h6 class="card-text">Durum:Uygun</h6><br>
                                <h5 class="card-title">İki Kişilik İkiz Yataklı Oda</h5>
                                <h5 class="card-title">Oda Tipi:Aile</h5>
                                <h6 class="card-text">399₺ / Gecelik</h6><br>
                                <h6 class="card-text">Kapasite: İki Kişilik</h6><br>
                                <h6 class="card-text">Yatak: Kral Yatak</h6><br>
                                <h6 class="card-text">Servis: İnternet, Televizyon, Duş</h6><br>
                                <a href="#" class="btn btn-info">Odayı Seç</a>
                            </div>
                        </div>
                    </div>
                }-->
                @foreach (var item in Model)
                {
                    <div class="col-xl-4 py-4">
                        <div style="border-radius: 10px;" class="card mx-auto" style="width: 18rem;">
                            <img class="card-img-top " src="~/images/oda1.jpg" alt="Card image cap">
                            <div class="card-body">
                                <h6 class="card-text">Durum:@item.Status.RoomStatusName</h6><br>
                                <h5 class="card-title">@item.OtherType.OtherTypeName</h5>
                                <h5 class="card-title">Oda Tipi:@item.RoomType.RoomTypeName</h5>
                                <h6 class="card-text">@item.OtherType.BasePrice₺ / Gecelik</h6><br>
                                <h6 class="card-text">Kapasite: @item.OtherType.Capacity</h6><br>
                                <h6 class="card-text">Yatak: @item.OtherType.BedType</h6><br>
                                <h6 class="card-text">Servis: @item.OtherType.Services</h6><br>
                                <a href="#" class="btn btn-info">Odayı Seç</a>
                            </div>


                        </div>


                    </div>
                }
            </div>
        </div>
    </div>
</div>
<!--ODA BİTİŞ-->

Model

Rena
  • 30,832
  • 6
  • 37
  • 72
  • Please Check [enter link description here](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Abhin May 10 '21 at 14:32
  • db.Rooms.Where is returning an Error, debug and see what the error is and that should lead you to a solution. – Harv May 10 '21 at 15:29
  • @Harv The contents of my other models are not coming, as far as I understand [image](https://i.imgur.com/PnJ0FAM.png) – Hüseyin Dönmez May 10 '21 at 15:34
  • Hi @Hüseyin Dönmez, you need return `List` instead of `List`. Your model contains other model,so you need use `.Include()` to append the related model. Check my answer below. – Rena May 11 '21 at 09:35

3 Answers3

1

Your are projecting your Rooms into a new anonymous object with values by calling

.Select(r => new { r.RoomId, r.OtherType.OtherTypeName, r.OtherType.BasePrice, r.OtherType.Capacity, r.OtherType.BedType, r.OtherType.Services, r.RoomType.RoomTypeName, r.Status.RoomStatusName })

So now you got an IEnumerable of anonymous type. But the action expects to return an IEnumerable of Rooms. Just remove the select you should be good, since you are retrieving from rooms table.

Zokka
  • 147
  • 6
  • `IEnumerable rm; rm = db.Rooms.Where(z => z.OtherTypeId == z.OtherType.OtherTypeId && z.RoomTypeId == z.RoomType.RoomTypeId && z.StatusId == z.Status.RoomStatusId).ToList(); return View(rm);` [I made changes and got an error like this](https://i.imgur.com/1IAqMMX.png) – Hüseyin Dönmez May 10 '21 at 15:03
  • Well check your view then. Looks like a property is null from which you are trying to access values from. Maybe your data within the table is null or not set/loaded. – Zokka May 10 '21 at 15:08
  • [image](https://i.imgur.com/PnJ0FAM.png) The contents of my other models are not coming, as far as I understand. – Hüseyin Dönmez May 10 '21 at 15:23
0

The List you are passing to view that should be type of Room Class.

Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
0

You need use .Include() method to include the related models like below:

var model = db.Rooms
    .Where(z => z.OtherTypeId == z.OtherType.OtherTypeId && z.RoomTypeId == z.RoomType.RoomTypeId && z.StatusId == z.Status.RoomStatusId)
    .Include(r=>r.OtherType)
    .Include(r=>r.RoomType)
    .Include(r=>r.Status)
    .ToList();
Rena
  • 30,832
  • 6
  • 37
  • 72