0
votes

I wanted to get invoice items from the database,But there's a problem in IEnumerable Type

Error
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.InvoiceHD_19624A0A21E23E6589B4A75F4B214A34A6186F3A2FF588A6B9CC2A40272A9BBD', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ICSNew.Data.InvoiceHD]'.

My Controller

public ActionResult GetInvoice(int Id)
{
    Models.InvoiceHDViewModel _invHd = new Models.InvoiceHDViewModel();

    ICSNew.Data.InvoiceHD _invOdr = new Data.InvoiceHD();
    _invOdr = (new ICSNew.Business.ICSNewController.SalesController())
                  .GetInvoiceItemByInvoiceId(Id);
    _invHd.Cash = _invOdr.CashAmount;

    return View(_invOdr);
}

My View

@model IEnumerable<ICSNew.Data.InvoiceHD>

@{
    Layout = null;
}

<h2>GetInvoice</h2>

@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.CashAmount)
}

My Repository

public InvoiceHD GetInvoiceItemByInvoiceId(int InvoiceId)
{
    try
    {
        return context.InvoiceHDs.Where(x => x.InvoiceId == InvoiceId 
            && x.IsActive == true).FirstOrDefault();
    }
    catch (Exception ex)
    {
        return new InvoiceHD();
    }
}
2

2 Answers

2
votes

Since you pass an instance of InvoiceHD to the view, you need to change the model as InvoiceHD and remove the foreach block in your view code as below

@model ICSNew.Data.InvoiceHD

@{
    Layout = null;
}

<h2>GetInvoice</h2>

@Html.DisplayFor(m => m.CashAmount)
1
votes

As the error is trying to tell you, you declared your view as taking a collection (IEnumerable<>) of objects, but you tried to pass it a single object instance.

That won't work.