使用参数

时间:2017-04-02 17:00:12

标签: c# asp.net-core entity-framework-core

使用带参数的存储过程显示我的视图时出现了一点问题。

这是我的代码:

查看



@model IEnumerable<PEMCOLoan.DAL.Entities.spModels.getLoanWithTypes>

@{ 
    ViewBag.Title = "List of Summary per Loan Types";
}

<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#home">Educational Loan</a></li>
    <li><a data-toggle="tab" href="#menu1">Petty Cash</a></li>
    <li><a data-toggle="tab" href="#menu2">Business Loan</a></li>
</ul>

<div class="tab-content">
    <div id="home" class="tab-pane fade in active">
        <h3>Educational Loan</h3>
        <table class="table">
            <tr>
                <th>Employee ID</th>
                <th>Name</th>
                <th>Loan Type</th>
                <th>Principal</th>
                <th>Interest</th>
                <th>Total</th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.EmployeeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.FullName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanTypeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.PrincipalAmount)</td>
                    <td>@Html.DisplayFor(modelItem => item.Interest)</td>
                    <td>@Html.DisplayFor(modelItem => item.Total)</td>
                </tr>

            }
        </table>
    </div>
    <div id="menu1" class="tab-pane fade">
        <h3>Petty Cash</h3>
        <table class="table">
            <tr>
                <th>Employee ID</th>
                <th>Name</th>
                <th>Loan Type</th>
                <th>Principal</th>
                <th>Interest</th>
                <th>Total</th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.EmployeeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.FullName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanTypeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.PrincipalAmount)</td>
                    <td>@Html.DisplayFor(modelItem => item.Interest)</td>
                    <td>@Html.DisplayFor(modelItem => item.Total)</td>
                </tr>

            }
        </table>
    </div>
    <div id="menu2" class="tab-pane fade">
        <h3>Business Loan</h3>
        <table class="table">
            <tr>
                <th>Employee ID</th>
                <th>Name</th>
                <th>Loan Type</th>
                <th>Principal</th>
                <th>Interest</th>
                <th>Total</th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.EmployeeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.FullName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanTypeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.PrincipalAmount)</td>
                    <td>@Html.DisplayFor(modelItem => item.Interest)</td>
                    <td>@Html.DisplayFor(modelItem => item.Total)</td>
                </tr>

            }
        </table>
    </div>
</div>
&#13;
&#13;
&#13;

我想要的是,我想检查一下它与桌子相处的贷款类型。例如,如果我有贷款类型1,2,3,如果贷款类型1将与Educational Loan的记录一起,2将与小额现金等等...这里是我的图片从事。 Educational Loan其他小组的情况也是如此。

控制器

现在我暂时有这个

        [HttpGet]
    public IActionResult Index()
    {
        return View(_Context.Set<getLoanWithTypes>().FromSql("sp_GetLoanTypesWithPrincipal").AsNoTracking());
    }

以下是我的带参数的部分代码

        [HttpGet]
    public IActionResult Index(getLoanWithTypes glwt)
    {



        return View(_Context.Set<getLoanWithTypes>().FromSql("sp_GetLoanTypesWithPrincipal @p0 = {0}, @p1 = {1}", glwt.EmployeeID.ToString(), glwt.LoanTypeID.ToString()).AsNoTracking());
    }

我有一点问题,因为我不知道如何从视图中传递参数。我想要做的是我想在视图加载时为每个LoanTypeID设置参数。或者只需单击每个面板,它就会设置LoanTypeID参数。

SQL: 参数:的     @empID AS INT = NULL,     @loanID AS INT = NULL

查询:

SELECT
[EmployeeID] = emp.EmployeeID,
[FullName] = emp.FName + ' ' + emp.LName,
[LoanTypeID] = lt.LoanTypeID,
[PrincipalAmount] = (SELECT ISNULL(SUM((CAST(((lc.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * 12)  * 2)) AS DECIMAL(18,2)))),0) FROM LoanContract lc INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID WHERE lc.LoanTypeID = @loanID AND lc.EmployeeID = emp.EmployeeID),
[Interest] = (SELECT ISNULL(SUM((CAST((((lt.InterestRate/100) * lc.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * 12)  * 2)) AS DECIMAL(18,2)))),0) FROM LoanContract lc INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID WHERE lc.LoanTypeID = @loanID AND lc.EmployeeID = emp.EmployeeID),
[Total] = (SELECT ISNULL(SUM((CAST(((((lt.InterestRate/100) * lc.LoanAmount) + lc.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(VARCHAR(15), LoanDateStart, 101), CONVERT(VARCHAR(15), LoanPaymentDue, 101)) * 12)  * 2)) AS DECIMAL(18,2)))),0) FROM LoanContract lc INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID WHERE lc.LoanTypeID = @loanID AND lc.EmployeeID = emp.EmployeeID)
FROM Employee emp
    INNER JOIN Salary sal ON sal.EmployeeID = emp.EmployeeID
    INNER JOIN LoanContract lc ON lc.EmployeeID = emp.EmployeeID
    INNER JOIN LoanType lt ON lt.LoanTypeID = lc.LoanTypeID
    LEFT JOIN LoanPayments lp ON lp.LoanID = lc.LoanID
    LEFT JOIN LoanFrequency lf ON lf.LoanFrequencyID = lc.LoanFrequencyID
WHERE emp.EmployeeID IN (SELECT EmployeeID FROM Employee WHERE EmployeeID LIKE '%' + CASE WHEN ISNULL(@empID,0) <> 0 THEN CAST(ISNULL(@empID, 0) AS VARCHAR(15)) ELSE '' END + '%')
    AND lc.LoanTypeID IN (SELECT LoanTypeID FROM LoanType WHERE LoanTypeID LIKE '%' + CASE WHEN ISNULL(@loanID,0) <> 0 THEN CAST(ISNULL(@loanID, 0) AS VARCHAR(15)) ELSE '' END + '%')
GROUP BY emp.EmployeeID, lt.LoanTypeID, emp.FName, emp.LName
ORDER BY emp.EmployeeID ASC

它的作用是每个员工获得当前的PrincipalAmountInterest等等......如果PrincipalAmount具有相同的LoanTypeID和EmployeeID,那么它将计算和总和。以下是示例图片Query Sample Picture

我对asp.net核心和ef核心有点新意,还没有先进的知识。

希望这足以满足一切。任何好的参考阅读或建议将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

  

您可以构建__init__()并将其作为参数值提供。   这允许您在SQL查询字符串中使用命名参数

Counter

框架将匹配参数直到命令。

参考 - Adding a single character to add keys in Counter