传递给 ViewDataDictionary 的模型项是 X[] 类型,但此 ViewDataDictionary 实例需要 X 类型的模型项

Doc*_*und 11 c# asp.net-mvc

错误:

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

你好,
在盯着一个错误几天并做了一些研究之后,我确定我对 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);
}
Run Code Online (Sandbox Code Playgroud)

查看:(文件名为“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>
Run Code Online (Sandbox Code Playgroud)

部分视图“_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>
Run Code Online (Sandbox Code Playgroud)

模型“票”:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我通读了位于此处的精彩帖子,虽然我认为这在我理解正在发生的事情的许多方面帮助了我很多,但我仍然发现错误难以解决。

SO *_*ood 14

问题是 View 使用的 Layout 需要一个模型,Ticket并且您正在传递它Ticket[](请注意,这是在您使用时隐式完成的return View(model))。

通常,由于 Layout 通过多个 View 使用,因此 Layout 不会声明@model. @model从布局中删除将解决您的问题。

  • 只是想对这个答案表示感谢,因为这个答案最近咬了我。我在测试某些东西时在布局中声明了一个模型,然后忘记了它。它工作了很长一段时间,因为它加载的部分声明了相同的模型,但是当我更改部分的模型时,它开始失败,而且根本不明显发生了什么! (2认同)