C# 传递到ViewDataDictionary的模型项的类型为错误
我的应用程序可能不好,我正在努力学习 这个错误的解决方案是什么? 控制器 我不知道该怎么做,我试过的例子都不管用 我想我得列个清单C# 传递到ViewDataDictionary的模型项的类型为错误,c#,entity-framework,model-view-controller,asp.net-core-mvc,C#,Entity Framework,Model View Controller,Asp.net Core Mvc,我的应用程序可能不好,我正在努力学习 这个错误的解决方案是什么? 控制器 我不知道该怎么做,我试过的例子都不管用 我想我得列个清单 public IActionResult Rooms() { using (var db = new HotelWebContext()) { var roomList = db.Rooms.Where(z => z.OtherTypeId == z.
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);
}
}
模型
我用实体框架自动创建了它
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; }
}
公共部分教室
{
公共房间()
{
保留=新的HashSet();
}
public int RoomId{get;set;}
public int?RoomTypeId{get;set;}
public int?OtherTypeId{get;set;}
public int?StatusId{get;set;}
公共虚拟OtherType OtherType{get;set;}
公共虚拟RoomType RoomType{get;set;}
公共虚拟房间状态{get;set;}
公共虚拟ICollection保留{get;set;}
}
Rooms.cshtml有一个foreach循环
@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 IEnumerable
@{
ViewData[“Title”]=“Odalarımız”;
ViewData[“暗”]=“文本暗”;
ViewData[“信息”]=“文本信息”;
}
奥达拉米兹
@foreach(模型中的var项目)
{
Durum:@item.Status.RoomStatusName
@item.OtherType.OtherTypeName
Oda Tipi:@item.RoomType.RoomTypeName
@item.OtherType.BasePrice₺ / 盖塞利克
Kapasite:@item.OtherType.Capacity
Yatak:@item.OtherType.BedType
服务:@item.OtherType.Services
}
您正在通过调用
.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 })
现在你得到了一个匿名类型的IEnumerable。
但该行动预计将返回无数房间。
只需删除select就可以了,因为您正在从rooms表中检索。要传递给视图的列表应该是Room类的类型。您需要使用
.Include()
方法来包含相关模型,如下所示:
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();
请检查db.Rooms.Where正在返回错误,调试并查看错误是什么,这将引导您找到解决方案。@Harv我的其他模型的内容不会出现,据我所知Hi@Hüseyin Dönmez,您需要返回
列表
,而不是列表
。您的模型包含其他模型,因此需要使用.Include()
附加相关模型。检查下面我的答案。IEnumerable rm;rm=db.Rooms.Where(z=>z.OtherTypeId==z.OtherType.OtherTypeId&&z.RoomTypeId==z.RoomType.RoomTypeId&&z.StatusId==z.Status.RoomStatusId==z.Status.RoomStatusId)。ToList();返回视图(rm)代码>然后检查您的视图。您试图从中访问值的属性似乎为null。表中的数据可能为空或未设置/加载。据我所知,我的其他模型的内容不会出现。我想我还有很多东西要学。多谢各位