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`1[Used_Clot

Analyst_SQL 3,576 Reputation points
2023-08-22T05:51:51.77+00:00

I am getting error .

Model.

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


}


View

@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">Section</label>
            <select asp-for="@Model.ToList()[0].SecId" class="form-select-sm form-control" data-live-search="true" asp-items="@(new SelectList(@Model.ToList()[0].Listofsections, "Value", "Text"))">
            </select>
        </div>
  
    </form>




    <div class="card-body">

        <a asp-action="Create" class="btn btn-primary" asp-controller="Items">
            Create New
        </a>
        <hr />


        <table class="table table-striped my-4 w-100" id="datatable2">

            <thead>
                <tr>
                    <th>

                        @Html.DisplayNameFor(model => model.CodeItem)
                    </th>
                    <th>

                        @Html.DisplayNameFor(model => model.Descriptionitem)
                    </th>


                    <th>

                        @Html.DisplayNameFor(model => model.Secnam)
                    </th>
                    <th>

                        @Html.DisplayNameFor(model => model.Cname)
                    </th>


                    <th>

                        @Html.DisplayNameFor(model => model.BaleSize)
                    </th>
                    <th>

                        @Html.DisplayNameFor(model => model.Weight)
                    </th>


                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                @if (Model != null && Model.Any())
                {
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.CodeItem)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Descriptionitem)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Secnam)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Cname)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.BaleSize)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Weight)
                            </td>

                            <td>
                                @*    @Html.ActionLink("Edit", "Edit", new { dept_id = item.SecId }, new { @class = "btn btn-warning" }) |

                        @Html.ActionLink("Delete", "Delete", new { dept_id = item.SecId }, new { @class = "btn btn-danger" }) |
                        *@
                                <a asp-action="Edit" asp-route-id="@item.CodeItem" class="btn btn-warning">Edit</a> |
                                <a asp-action="Delete" asp-route-id="@item.CodeItem" class="btn btn-danger">Delete</a>

                            </td>
                        </tr>
                    }

                }

                else
                {
                    <tr>
                        <td colspan="6">
                            <div>
                                No Employee Data is available..

                            </div>


                        </td>
                    </tr>
                }

            </tbody>

        </table>


    </div>


@*   <div>
        <partial name="_GetItem" model="Model" />
   </div>*@


    </div>


Action Controller

 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);
        }
Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-08-22T07:40:15.77+00:00

    Hi,@akhter hussain

    just as the error indicates,your view expects

    IEnumerable<Used_Clothing_Managment.Models.ItemMasterFile>

    but the viewmodel you passed form controller to view is type of ItemMasterFile

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

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Ruikai Feng


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-08-22T10:14:16.1833333+00:00

    Ok,I could understand your question well ,since you called@model Enumerable<Used_Clothing_Managment.Models.ListView.ItemMV> in your view,the true question for you is how to pass the selectlist together with ItemMVs to your view .Isn't it?

    I suggest you try as below:

    in controller

    var Itemlist =//query in db....
    ....
    ViewBag.SelctList=sectionList
    return View(Itemlist);
    

    In view:

    <select asp-items="@ViewBag.Selectlist"..........></select>

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.