传递到 ViewDataDictionary 的模型项的类型为 X[],但此 ViewDataDictionary 实例需要类型为 X 的模型项[重复]

2024-03-17

Error:

InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“Test.Models.Ticket[]”,但此 ViewDataDictionary 实例需要类型为“Test.Models.Ticket”的模型项。

Hello,
在盯着一个错误几天并做了一些研究之后,我确定我对 MVC 的了解还不足以解决这个问题。我足够了解我的控制器正在尝试将数组传递给视图,但视图并不寻找数组(?)。下面是代码:

控制器:

//this is supposed to take the last five "tickets" created from a database
[Route("Ticket")]
public IActionResult Index(int page = 0)
{
    var model = _db.Tickets.OrderByDescending(x => x.Time).Take(5).ToArray();
    return View(model);
}

View:(文件标题为“Index.cshtml”,位于“Views”文件夹内的“Ticket”文件夹下)

@model IEnumerable<Test.Models.Ticket>
@{
    Layout = "_Layout";
}

<p>Below is supposed to display the latest 5 "tickets" from a database.</p>

<div class="ticket-create">
    @foreach (var ticket in Model)
    {
        @Html.Partial("_Ticket", ticket)
    }
</div>

部分视图“_Ticket”:

@model Test.Models.Ticket

<article class="ticket-create">
    <h1>@Html.ActionLink(Model.Type, "Create", "Ticket", new { year = Model.Time.Year, month = Model.Time.Month, day = Model.Time.Day, time = Model.Time.TimeOfDay, key = Model.Key })</h1>
    <div class="type">
        Created on <span>@Model.Time</span> by <span>@Model.Name</span>
    </div>
    <div class="desc">
        @Model.Desc
    </div>
    <div class="clearance">
        @Model.Clearance
    </div>
</article>

型号“票”:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Test.Models
{
    public class Ticket
    {
        public long Id { get; set; }

        private string _key;

        public string Key
        {
            get
            {
                if(_key == null)
                {
                    _key = Regex.Replace(Name.ToLower(), "[^a-z0-9]", "-");
                }
                return _key;
            }
            set { _key = value; }
        }

        [Required]
        [DataType(DataType.Text)]
        public string Type { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public string Name { get; set; }

        [StringLength(300, MinimumLength = 5, ErrorMessage = "The description must be between 5 and 300 characters long.")]
        [DataType(DataType.MultilineText)]
        public string Desc { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime Time { get; set; }

        [Required]
        [DataType(DataType.Text)]
        public string Clearance { get; set; }

        [DataType(DataType.Text)]
        public string Author { get; set; }
    }
}

_Layout.cshtml 声明以下模型:“@model Test.Models.Ticket”

我读了一篇精彩的帖子,位于here https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ,但是虽然我认为这在我理解正在发生的事情的许多方面帮助了我很多,但我仍然发现这个错误很难解决。


问题是视图正在使用的布局需要一个模型Ticket你正在传递它Ticket[](请注意,当您使用时,这是隐式完成的return View(model)).

通常,由于 Layout 是通过多个 View 使用的,所以 Layout 不会声明一个@model。删除@model从布局将解决您的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

传递到 ViewDataDictionary 的模型项的类型为 X[],但此 ViewDataDictionary 实例需要类型为 X 的模型项[重复] 的相关文章

随机推荐