模型項傳遞到ViewDataDictionary

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

嗨,我的代碼有問題,似乎它與實例有關但我聲明已經在控制器中的一個實例,它似乎無法正常工作。

我收到了這個錯誤:

InvalidOperationException:傳遞給ViewDataDictionary的模型項的類型為'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [PEMCOLoan.DAL.Entities.spModels.spGetLedger]',但此ViewDataDictionary實例需要類型為'PEMCOLoan的模型項。 Web.Models.Ledger“。

調節器

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

查看

@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>

熱門答案

AsNoTracking返回IQueryable。將其更改為.AsNoTracking()。ToList()以強制在該點進行檢索。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow