InvalidOperationException:传递到 ViewDataDictionary 的模型项类型为“Used_Clothing_Managment.Models.ItemMasterFile”,但此 ViewDataDictionary 实例需要类型为“System.Collections.Generic.IEnumerable”的模型项 1[Used_Clot

Hui Liu-MSFT 48,511 信誉分 Microsoft 供应商
2024-05-24T08:29:09.4833333+00:00

我收到错误。

型。

using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using Used_Clothing_Managment.Models.ListView;

namespace Used_Clothing_Managment.Models;

public  class ItemMasterFile
{
    public int CodeItem { get; set; }

    public string? Descriptionitem { get; set; }

    public string? BaleSize { get; set; }

    public int? Weight { get; set; }

    public int? SecId { get; set; }

    public string? Packsize { get; set; }

    public string? Alid { get; set; }

    public int? Iduom { get; set; }

    public DateTime? EntryDate { get; set; }

    public int? Cid { get; set; }

    public string? Icn { get; set; }

    public int? TiD { get; set; }

    public int? Rid { get; set; }

    public DateTime? Udate { get; set; }

    public string? Ipaddress { get; set; }

    public int? Delid { get; set; }

    public string? IStatus { get; set; }

    public TimeSpan? UTime { get; set; }

    public string? ICat { get; set; }

    public string? IHot { get; set; }

    public decimal? YPer { get; set; }

    public int? WType { get; set; }

    public virtual ICollection<Bigbalprd> Bigbalprds { get; set; } = new List<Bigbalprd>();

    public virtual ICollection<CustomerItem> CustomerItems { get; set; } = new List<CustomerItem>();

    public virtual Uom? IduomNavigation { get; set; }

    public virtual Region? RidNavigation { get; set; }

    public virtual Section? Sec { get; set; }

    public virtual ItemType? TiDNavigation { get; set; }
  
    
    [NotMapped]

    public List<SelectListItem>? Listofsections { get; set; }


    [NotMapped]
    public List<SelectListItem>? Listofcategory { get; set; }



    [DisplayName("Section")]

    public string? Secnam { get; set; }


    [DisplayName("Category")]

    public string? Cname { get; set; }


}


视图

@model IEnumerable<Used_Clothing_Managment.Models.ItemMasterFile>


@{
    ViewBag.Title = "Item List";
}
    <div class="card-body">
        @*  @Html.ActionLink("Create Department", "CreateDepartment", null, new { @class = "btn btn-primary" })*@
        <a asp-action="Create" class="btn btn-primary" asp-controller="Items">
            Create New
        </a>
        <hr />
  

    <form asp-action="Create">


        <div class="form-group">
            <div class="alert-danger" asp-validation-summary="ModelOnly"></div>
            <label asp-for="@Model.ToList()[0].SecId

动作控制器

 public IActionResult ItemList()
        {

            var Itemlist = (from i in _context.ItemMasterFiles
                            join s in _context.Sections on i.SecId equals s.SecId
                            join c in _context.Catagories on i.Cid equals c.Cid

                            into ce
                            from sub in ce.DefaultIfEmpty()
                            where i.Packsize == "1" || i.Delid == null
                            select new ItemMV()
                            {
                                CodeItem = i.CodeItem,
                                Descriptionitem = i.Descriptionitem,
                                Secnam = s.Secnam,

                                BaleSize = i.BaleSize,
                                Weight = i.Weight,
                                Cname = sub.Cname,

                            }).ToList();


            ItemMasterFile seclist = new ItemMasterFile();

            var sectionList = (from s in _context.Sections

                               select new SelectListItem()
                               {
                                   Text = s.Secnam,
                                   Value = s.SecId.ToString()
                               }).ToList();

            sectionList.Insert(0, new SelectListItem()
            {
                Text = "----Select----",
                Value = string.Empty
            });

            seclist.Listofsections = sectionList;
      

            return View(seclist);
        }


Note:此问题总结整理于:[InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Used_Clothing_Managment.Models.ItemMasterFile', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1Used_Clot

Entity Framework Core
Entity Framework Core
实体框架数据访问技术的轻量型、可扩展、开源、跨平台版本。
49 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 44,756 信誉分 Microsoft 供应商
    2024-05-24T09:07:05.73+00:00

    嗨,@akhter侯赛因

    正如错误所示,您的视图期望

    IEnumerable<Used_Clothing_Managment.Models.ItemMasterFile>

    但是您传递给 Form Controller 的 viewModel 的类型为ItemMasterFile

    ItemMasterFile seclist = new ItemMasterFile();
    .....
    // you passed a single ItemMasterFile object instead of  IEnumerable<ItemMasterFile> to your view here
    return View(seclist);
    

    如果要在视图中显示多重 ItemMasterFile 项

    您可以尝试以下方法:

    var viewmodel = new List<ItemMasterFile>();
    ItemMasterFile seclist = new ItemMasterFile();
    .........
     viewmodel.Add(seclist);
    return View(viewmodel);
    
    
    

    如果只想在视图中显示一个 ItemMasterFile 项,

    只需修改您的视图:

    @model IEnumerable<Used_Clothing_Managment.Models.ItemMasterFile> =>@model Used_Clothing_Managment.Models.ItemMasterFile

    @Model.ToList()[0].SecId=>@Model.SecId

    
    @if (Model != null && Model.Any())
    {
        @foreach (var item in Model)
           {
              <tr>
                <td>
                     @Html.DisplayFor(modelItem => item.CodeItem)
                </td>
    ........
           }
    }
    

    =>

    @Html.DisplayFor(modelItem
    

    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。