The model item passed into the ViewDataDictionary

asp.net-core c# entity-framework-core

Question

Hi I have problem in my code and it seems it has something to do with the instances but I declare already a instance in the controller and it seems it is not working.

I got this error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[PEMCOLoan.DAL.Entities.spModels.spGetLedger]', but this ViewDataDictionary instance requires a model item of type 'PEMCOLoan.Web.Models.Ledger'.

Controller

[HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult getEmployeeLedger(spGetLedger ledger)
    {
        var firstDay = _Context.LoanPayments.Min(min => min.DateOfPayment);
        var lastDay = _Context.LoanPayments.Max(max => max.DateOfPayment);

        //Set Test data

        //Set Parameter
        var StartDate = new SqlParameter("StartDate", firstDay);
        var EndDate = new SqlParameter("EndDate", lastDay);
        var EmpID = new SqlParameter("EmployeeID", ledger.EmployeeID);
        var LoanTypeID = new SqlParameter("LoanType", ledger.LoanTypeID);

        Ledger newLedger = new Ledger();

        newLedger.enumledger = _Context.Set<spGetLedger>().FromSql("EXECUTE spGetLedger @EmployeeID, @LoanType, @StartDate, @EndDate", EmpID, LoanTypeID, StartDate, EndDate).AsNoTracking();
        //var empLedger = _Context.getLedger
        return View(newLedger.enumledger);
    }

Views

@model PEMCOLoan.Web.Models.Ledger

@{ 
    ViewBag.Title = "Ledger";
}

<h2>View @Model.ledger.FullName's Ledger</h2>
<h3>Loan Name: @Model.ledger.LoanName</h3>
<h3>Total Loan Amount: @Model.ledger.TotalLoanAmount</h3>

@if (Model.enumledger.Any())
{
    <table class="table table-responsive">
        <tr>
            <th>Date Paid</th>
            <th>Principal Amount</th>
            <th>Interest</th>
            <th>Total</th>
        </tr>
        @foreach(var item in Model.enumledger)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.PaymentDate)</td>
                <td>@Html.DisplayFor(modelItem => item.TotalLoanAmount)</td>
                <td>@Html.DisplayFor(modelItem => item.Interest)</td>
                <td>@Html.DisplayFor(modelItem => item.Total)</td>
            </tr>
        }
    </table>
}
<h3>Balance: @Model.ledger.Balance</h3>

1
0
4/28/2017 8:28:48 AM

Popular Answer

AsNoTracking returns IQueryable. Change it to .AsNoTracking().ToList() to force the retrieval at that point.

2
4/28/2017 8:26:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow