The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[DB_Payment.Models.GetInvoice]', but this dictionary requires a model item of type 'DB_Payment.Models.GetInvoice'. C# MVC

Vinisha 1 Reputation point
2021-08-13T04:08:50.963+00:00

Hi,
Getting an error in C# MVC application:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[DB_Payment.Models.GetInvoice]', but this dictionary requires a model item of type 'DB_Payment.Models.GetInvoice'.

Controller:
List<DB_Payment.Models.GetInvoice> Items = new List<DB_Payment.Models.GetInvoice>();
DataSet ds = new DataSet();
ds = GetItems(customer_code);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
var chk = new GetInvoice();
chk.customer_code = dr["customer_code"].ToString();
chk.Date_Due = dr["Date_Due"].ToString();
chk.amount = dr["amount"].ToString();
chk.amt_paid = dr["amt_paid"].ToString();
Items.Add(chk);

            }  
        }  

        return View(Items);  

View Code:
@默 IEnumerable<DB_Payment.Models.GetInvoice>
@using GridMvc.Html
@using System.Web.Mvc.Ajax

@{

ViewBag.Title = "grid";  
WebGrid grid = new WebGrid(Model, rowsPerPage: 100);  
Layout = "~/Views/Shared/_Layout.cshtml";  

}

<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<h4 class="card-title mb-4">Inventory</h4>
</div>

            </div>  
            <div id="webgrid">  
                @Html.Grid(Model).Columns(columns =>  
                {  

                    columns.Add(m => m.customer_code).Titled("customer_code");                         
                    columns.Add(m => m.Date_Due).Titled("Date_Due");  
                    columns.Add(m => m.amount).Titled("Amount");  
                    columns.Add(m => m.amt_paid).Titled("Amt_Paid");  

                }  
                )  

            </div>  
        </div>  
    </div>  
</div>  

</div>

can somebody please help?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,061 Reputation points
    2021-08-13T07:00:03.777+00:00

    Hi @Vinisha ,
    I have tested your codes and I can't reproduce your problems.
    I think your codes you posted to us shouldn't have this error. This error means the value's type don't match. However, In the controller, you post the value type is a list. And in the view page, you used IEnumerable. It's right. So,which lines causes the error? Could you post other codes to us?
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.

    0 comments No comments

  2. Sanhita Roy 1 Reputation point
    2022-01-24T17:26:14.38+00:00

    I am getting the same error with the following code

    public ActionResult SpecificationOptions(string referenceNo,string requestID)
    {
    FDEDownload.Models.FDEDownloadIndividualContact specification;
    FDEDownload.Models.FDEDownloadFireRatings fireRatings;
    FDEDownload.Models.FDEDownloadBSENISO bSENISO;
    FDEDownloadSiteLocations location;

            try  
            {  
                specification = new FDEDownload.Models.FDEDownloadIndividualContact();  
                fireRatings = new Models.FDEDownloadFireRatings();  
                bSENISO = new Models.FDEDownloadBSENISO();  
                location = new FDEDownloadSiteLocations();  
    
                ViewData["ReferenceNo"] = referenceNo;  
                ViewData["FDEDownloadRequestID"] = Convert.ToInt32(requestID);  
                string site = location.GetFDEDownloadSiteLocation();  
                ViewData["SiteLocation"] = site;  
                Session["FireRating"] = fireRatings.GetFDEDownloadFireRating();  
                Session["BSENISO"] = bSENISO.GetFDEDownloadBSENISO();  
    
                specification.FDEDownloadFireRatingList = fireRatings.GetFDEDownloadFireRating();  
                return View("~/Views/FDEDownload/SpecificationOptions.cshtml", specification);  
            }  
            catch (Exception ex)  
            {  
                ErrorLog.GetDefault(null).Log(new Error(ex));  
                throw;  
            }  
        }  
    

    Then the specification view

    @默 FDEDownload.Models.FDEDownloadIndividualContact

    0 comments No comments