您好我的代码有问题,似乎它与实例有关但我在控制器中声明已经是一个实例,它似乎无效。
我收到了这个错误:
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);
}
答案 0 :(得分:2)
AsNoTracking返回IQueryable。将其更改为.AsNoTracking()。ToList()以强制在该点进行检索。
答案 1 :(得分:1)
您需要添加.ToList()
:
Ledger newLedger = new Ledger();
newLedger.enumledger = _Context.Set<spGetLedger>().FromSql("EXECUTE spGetLedger @EmployeeID, @LoanType, @StartDate, @EndDate", EmpID, LoanTypeID, StartDate, EndDate).AsNoTracking().ToList();
return View(newLedger.enumledger);