【问题标题】:The model item passed into the ViewDataDictionary is of type Error传入 ViewDataDictionary 的模型项的类型为 Error
【发布时间】:2021-07-31 23:12:13
【问题描述】:

我正在尝试学习的应用程序可能很糟糕 这个错误的解决方案是什么? The error i got

控制器 我不确切知道如何制作列表,我尝试的示例不起作用 我想我需要以某种方式列出清单

     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; }
    }

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

【问题讨论】:

  • db.Rooms.Where 返回错误,调试并查看错误是什么,这应该会引导您找到解决方案。
  • @Harv 我其他模型的内容不来,据我了解image
  • 嗨@Hüseyin Dönmez,您需要返回List&lt;Room&gt; 而不是List&lt;anonymous&gt;。您的模型包含其他模型,因此您需要使用.Include() 附加相关模型。在下面检查我的答案。

标签: c# entity-framework model-view-controller asp.net-core-mvc


【解决方案1】:

您传递给 view 的 List 应该是 Room Class 的类型。

【讨论】:

    【解决方案2】:

    您需要使用.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();
    

    【讨论】:

    • 我想我还有很多东西要学。非常感谢
    【解决方案3】:

    你正在通过调用将你的房间投影到一个新的匿名对象中

    .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。 但是该操作期望返回一个 IEnumerable 房间。 只需删除您应该很好的选择,因为您正在从房间表中检索。

    【讨论】:

    • IEnumerable&lt;Room&gt; rm; rm = db.Rooms.Where(z =&gt; z.OtherTypeId == z.OtherType.OtherTypeId &amp;&amp; z.RoomTypeId == z.RoomType.RoomTypeId &amp;&amp; z.StatusId == z.Status.RoomStatusId).ToList(); return View(rm); I made changes and got an error like this
    • 那么请检查您的视图。看起来您尝试从中访问值的属性为 null。也许您在表中的数据为空或未设置/加载。
    • image据我了解,我其他模型的内容没有来。
    猜你喜欢
    • 2017-09-26
    • 2022-01-19
    • 2020-10-20
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2022-01-17
    相关资源
    最近更新 更多