return 部分视图剃须刀页面基于 属性

return partial view razor pages based on property

我试图 return 基于视图参数的一组不同的列,但我似乎无法使其工作:

@if (Model.ExpendituresList != null)
{
    <h2>Expenditure details</h2>
    <table class="table">

        @if (Model.ExpenditureFilter == "UnbilledLabor")
        {
            @Html.RenderPartialAsync("_LaborUnbilled", Model.ExpendituresList);
        }
        @if (Model.ExpenditureFilter == "UnbilledNonLabor")
        {
            @Html.RenderPartialAsync("_NonLaborUnbilled", Model.ExpendituresList);
        }

    </table>
}

和我的部分观点: _LaborUnbilled:

<thead>
<tr>
    <th>Task Number Unbilled NON LABOR</th>
    <th>Employee Name</th>
    <th>Employee Number</th>
    <th>Expenditure Type</th>
    <th>Item Date</th>
    <th>Period Name</th>
    <th>Billable</th>
</tr>
</thead>
<tbody>

@foreach (var expenditure in Model.ExpendituresList)
{
    <tr>
        <td>@expenditure.TaskNumber</td>
        <td>@expenditure.EmployeeName</td>
        <td>@expenditure.EmployeeNumber</td>
        <td>@expenditure.ExpenditureType</td>
        <td>@expenditure.ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
        <td>@expenditure.GlPeriodName</td>
        <td>@expenditure.BillableFlag</td>
    </tr>
}
</tbody>

_NonLaborUnbilled:

<thead>
<tr>
    <th>Task Number Unbilled NON LABOR</th>
    <th>Employee Name</th>
    <th>Employee Number</th>
    <th>Expenditure Type</th>
    <th>Item Date</th>
    <th>Period Name</th>
    <th>Billable</th>
</tr>
</thead>
<tbody>

@foreach (var expenditure in Model.ExpendituresList)
{
    <tr>
        <td>@expenditure.TaskNumber</td>
        <td>@expenditure.EmployeeName</td>
        <td>@expenditure.EmployeeNumber</td>
        <td>@expenditure.ExpenditureType</td>
        <td>@expenditure.ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
        <td>@expenditure.GlPeriodName</td>
        <td>@expenditure.BillableFlag</td>
    </tr>
}
</tbody>

就我所见,我的 Model.ExpenditureFilter 以 "" 的形式出现在视图中。我可能是我缺少正确传递此参数的方法?

这是我的完整代码和视图供参考(欢迎任何建议)

代码:

using Dapper;
using DataAccessLibrary.Data;
using DataAccessLibrary.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WorkInProgress.Pages
{
    public class Expenditures : PageModel
    {
        private readonly ISqlData _db;

        public Expenditures(ISqlData db)
        {
            _db = db;
        }

        [BindProperty] public Project Project { get; set; }
        public IEnumerable<Expenditure> ExpendituresList { get; set; }

        public CalendarPeriod CalendarPeriod { get; set; }

        [BindProperty] public WipCommentCalculation WipCommentCalculation { get; set; }

        [BindProperty]
        public string ExpenditureFilter { get; set; } = string.Empty;

        public Calculations Calculations { get; set; }


        [NonAction]
        public virtual PartialViewResult PartialView(string viewName, object model)
        {
            ViewData.Model = model;

            return new PartialViewResult()
            {
                ViewName = viewName,
                ViewData = ViewData,
                TempData = TempData
            };
        }

        public async Task OnGetAsync(int projectId, string ExpenditureFilter)
        {
            CalendarPeriod = await _db.GetCalendarPeriodAsync();

            Project = await _db.GetProjectDataAsync(projectId);

            var comments = await _db.GetExpenditureCommentsAndData(projectId, CalendarPeriod.GlPeriodOracle);

            Calculations = await _db.GetCalculationByProjectIdAsync(projectId);

            WipCommentCalculation = new WipCommentCalculation
            {
                Calculations = Calculations,
                ProjectId = Project.ProjectId,
                Comments = new CommentModel
                {
                    PALaborComment = comments?.PALaborComment,
                    PANonLaborComment = comments?.PANonLaborComment,
                    PASubContractorComment = comments?.PASubContractorComment,
                    PMLaborComment = comments?.PMLaborComment,
                    PMNonLaborComment = comments?.PMNonLaborComment,
                    PMSubcontractorComment = comments?.PMSubcontractorComment
                },
                ProjectNumber = Project.ProjectNumber,
                GlPeriodName = CalendarPeriod.GlPeriodOracle
            };

            var completeExpendituresList = await _db.GetExpenditureDataFromOracleByProjectIdAsync(projectId);

            ExpendituresList = ExpenditureFilter switch
            {
                "UnbilledLabor" => completeExpendituresList.Where(
                    x => x.ExpTxnCategory == "Labor" && x.ArInvNum == null),
                "UnbilledNonLabor" => completeExpendituresList.Where(x =>
                    x.ExpTxnCategory == "Non Labor" && x.ArInvNum == null),
                "UnbilledSubcontractor" => completeExpendituresList.Where(x =>
                    x.ExpTxnCategory == "Subcontracts" && x.ArInvNum == null),
                "BillingHoldLabor" => completeExpendituresList.Where(x =>
                    x.ExpTxnCategory == "Labor" && x.BillHoldFlag == "Y"),
                "BillingHoldNonLabor" => completeExpendituresList.Where(x =>
                    x.ExpTxnCategory == "Non Labor" && x.BillHoldFlag == "Y"),
                "BillingHoldSubcontractor" => completeExpendituresList.Where(x =>
                    x.ExpTxnCategory == "Subcontracts" && x.BillHoldFlag == "Y"),
                _ => ExpendituresList
            };
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid == false)
            {
                return Page();
            }

            await _db.InsertOrUpdateComments(WipCommentCalculation);

            return Page();
            //return RedirectToPage();
        }
    }
}

查看:

@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.Expenditures
@{
    ViewData["Title"] = "Expenditures";
}

<h1>Project Information</h1>

<div class="card text-dark bg-light mb-3">
    <div class="container">
        <div class="row">
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Project Number</h5>
                    <p class="card-text">@Model.Project.ProjectNumber</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Project Name</h5>
                    <p class="card-text">@Model.Project.Name</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Contract Type</h5>
                    <p class="card-text">@Model.Project.ContractType</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Currency</h5>
                    <p class="card-text">@Model.Project.ProjFuncCurrencyCode</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Project Accountant</h5>
                    <p class="card-text">@Model.Project.PaName</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Project Manager</h5>
                    <p class="card-text">@Model.Project.PmName</p>
                </div>
            </div>
        </div>

    </div>
</div>

<h2>Project Summary</h2>

<div class="card text-dark bg-light mb-3">
    <div class="container">
        <div class="row">
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Fiscal Month</h5>
                    <p class="card-text">@Model.Project.FiscalMonth</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Currency</h5>
                    <p class="card-text">@Model.Project.ProjFuncCurrencyCode</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Revenue</h5>
                    <p class="card-text">@($"{Model.Project.PtdRevenue:0,0.00}")</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Bill Amount</h5>
                    <p class="card-text">@($"{Model.Project.PtdBilled:0,0.00}")</p>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">Unbilled/Unearned (WIP Amount)</h5>
                    <p class="card-text">@($"{Model.Project.PtdUnbilled:0,0.00}")</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">POC Amount/Adjustment</h5>
                    <p class="card-text">@($"{Model.Project.PocAmount:0,0.00}")</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">WIP Figure (+/-)</h5>
                    <p class="card-text">@($"{Model.Project.WipAmount:0,0.00}")</p>
                </div>
            </div>
            <div class="col">
                <div class="card-body">
                    <h5 class="card-title">NB Labor</h5>
                    <p class="card-text">@($"{Model.Project.NBCostLaborPtd:0,0.00}")</p>
                </div>
            </div>
        </div>
    </div>
</div>

<form method="post">
    <div class="form-control">
        <h2>Expenditures</h2>
        <table class="table table-hover">
            <thead class = "table-light">
            <tr>
                <th></th>
                <th>Billable</th>
                <th>Unbilled</th>
                <th>Billing Hold</th>
                <th style="width: 25%">Project Accountant Explanation</th>
                <th style="width: 25%">Project Manager Comments</th>
            </tr>
            </thead>
            <tbody>
            @Html.HiddenFor(model => model.ExpenditureFilter)
            @Html.HiddenFor(model => model.WipCommentCalculation.GlPeriodName)
            @Html.HiddenFor(model => model.WipCommentCalculation.ProjectNumber)
            @Html.HiddenFor(model => model.WipCommentCalculation.ProjectId)
            <tr>
                <td>Labor</td>
                <td>@Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillableLabor) @($"{Model.WipCommentCalculation.Calculations.BillableLabor:0,0.00}")</td>
                <td><a asp-page="" asp-route-ExpenditureFilter="UnbilledLabor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledLabor) @($"{Model.WipCommentCalculation.Calculations.UnbilledLabor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldLabor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldLabor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldLabor:0,0.00}")</a></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1"></textarea></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PMLaborComment" rows="1"></textarea></td>
            </tr>
            <tr>
                <td>Non-Labor </td>
                <td>@Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillableNonLabor) @($"{Model.WipCommentCalculation.Calculations.BillableNonLabor:0,0.00}")</td>
                <td><a asp-page="" asp-route-ExpenditureFilter="UnbilledNonLabor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledNonLabor) @($"{Model.WipCommentCalculation.Calculations.UnbilledNonLabor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldNonLabor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldNonLabor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldNonLabor:0,0.00}")</a></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PANonLaborComment" rows="1"></textarea></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PMNonLaborComment" rows="1"></textarea></td>
            </tr>
            <tr>
                <td>SubContractor</td>
                <td>@Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillableSubcontractor) @($"{Model.WipCommentCalculation.Calculations.BillableSubcontractor:0,0.00}")</td>
                <td><a asp-page="" asp-route-ExpenditureFilter="UnbilledSubcontractor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledSubcontractor) @($"{Model.WipCommentCalculation.Calculations.UnbilledSubcontractor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldSubcontractor" asp-route-projectId=@Model.Project.ProjectId> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldSubcontractor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldSubcontractor:0,0.00}")</a></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PASubContractorComment" rows="1"></textarea></td>
                <td><textarea class="form-control" asp-for="WipCommentCalculation.Comments!.PMSubcontractorComment" rows="1"></textarea></td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
                <td>Totals</td>
                <td>@($"{(Model.WipCommentCalculation.Calculations.BillableLabor + Model.WipCommentCalculation.Calculations.BillableNonLabor + Model.WipCommentCalculation.Calculations.BillableSubcontractor):0,0.00}")</td>
                <td>@($"{(Model.WipCommentCalculation.Calculations.UnbilledLabor + Model.WipCommentCalculation.Calculations.UnbilledNonLabor + Model.WipCommentCalculation.Calculations.UnbilledSubcontractor):0,0.00}")</td>
                <td>@($"{(Model.WipCommentCalculation.Calculations.BillingHoldLabor + Model.WipCommentCalculation.Calculations.BillingHoldNonLabor + Model.WipCommentCalculation.Calculations.BillingHoldSubcontractor):0,0.00}")</td>
                <td></td>
                <td></td>
            </tr>
            </tfoot>
        </table>
    </div>
    <div class="form-control">
        <input type="submit" value="Save" class="btn btn-primary"/>
    </div>
</form>

@if (Model.ExpendituresList != null)
{
    <h2>Expenditure details</h2>
    <table class="table">

        @if (Model.ExpenditureFilter == "UnbilledLabor")
        {
            @Html.RenderPartialAsync("_LaborUnbilled", Model.ExpendituresList);
        }
        @if (Model.ExpenditureFilter == "UnbilledNonLabor")
        {
            @Html.RenderPartialAsync("_NonLaborUnbilled", Model.ExpendituresList);
        }

    </table>
}

@section Scripts
{
    @{ await Html.RenderPartialAsync("Shared/_ValidationScriptsPartial");}
}

默认情况下,属性未绑定到 HTTP GET 请求。在您确实希望属性绑定到来自 GET 请求的数据的情况下,将 SupportsGet 属性 设置为 true。

参考 - Microsoft docs.