Home > Enterprise >  return partial view razor pages based on property
return partial view razor pages based on property

Time:06-03

I was trying to return a different set of columns based on a parameter on a view, however I can't seem to make it work:

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

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

    </table>
}

and my partial views: _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>

For what I can see, my Model.ExpenditureFilter is coming as "" to the view. I might be that I'm missing a way to pass this parameter properly?

Here is my complete code and view for reference (any suggestion is welcome)

code:

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

view:

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

<h1>Project Information</h1>

<div >
    <div >
        <div >
            <div >
                <div >
                    <h5 >Project Number</h5>
                    <p >@Model.Project.ProjectNumber</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Project Name</h5>
                    <p >@Model.Project.Name</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Contract Type</h5>
                    <p >@Model.Project.ContractType</p>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <h5 >Currency</h5>
                    <p >@Model.Project.ProjFuncCurrencyCode</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Project Accountant</h5>
                    <p >@Model.Project.PaName</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Project Manager</h5>
                    <p >@Model.Project.PmName</p>
                </div>
            </div>
        </div>

    </div>
</div>

<h2>Project Summary</h2>

<div >
    <div >
        <div >
            <div >
                <div >
                    <h5 >Fiscal Month</h5>
                    <p >@Model.Project.FiscalMonth</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Currency</h5>
                    <p >@Model.Project.ProjFuncCurrencyCode</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Revenue</h5>
                    <p >@($"{Model.Project.PtdRevenue:0,0.00}")</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >Bill Amount</h5>
                    <p >@($"{Model.Project.PtdBilled:0,0.00}")</p>
                </div>
            </div>
        </div>
        <div >
            <div >
                <div >
                    <h5 >Unbilled/Unearned (WIP Amount)</h5>
                    <p >@($"{Model.Project.PtdUnbilled:0,0.00}")</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >POC Amount/Adjustment</h5>
                    <p >@($"{Model.Project.PocAmount:0,0.00}")</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >WIP Figure ( /-)</h5>
                    <p >@($"{Model.Project.WipAmount:0,0.00}")</p>
                </div>
            </div>
            <div >
                <div >
                    <h5 >NB Labor</h5>
                    <p >@($"{Model.Project.NBCostLaborPtd:0,0.00}")</p>
                </div>
            </div>
        </div>
    </div>
</div>

<form method="post">
    <div >
        <h2>Expenditures</h2>
        <table >
            <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" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledLabor) @($"{Model.WipCommentCalculation.Calculations.UnbilledLabor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldLabor" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldLabor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldLabor:0,0.00}")</a></td>
                <td><textarea  asp-for="WipCommentCalculation.Comments!.PALaborComment" rows="1"></textarea></td>
                <td><textarea  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" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledNonLabor) @($"{Model.WipCommentCalculation.Calculations.UnbilledNonLabor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldNonLabor" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldNonLabor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldNonLabor:0,0.00}")</a></td>
                <td><textarea  asp-for="WipCommentCalculation.Comments!.PANonLaborComment" rows="1"></textarea></td>
                <td><textarea  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" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.UnbilledSubcontractor) @($"{Model.WipCommentCalculation.Calculations.UnbilledSubcontractor:0,0.00}")</a></td>
                <td><a asp-page="" asp-route-ExpenditureFilter="BillingHoldSubcontractor" [email protected]> @Html.HiddenFor(model => model.WipCommentCalculation.Calculations.BillingHoldSubcontractor) @($"{Model.WipCommentCalculation.Calculations.BillingHoldSubcontractor:0,0.00}")</a></td>
                <td><textarea  asp-for="WipCommentCalculation.Comments!.PASubContractorComment" rows="1"></textarea></td>
                <td><textarea  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 >
        <input type="submit" value="Save" />
    </div>
</form>

@if (Model.ExpendituresList != null)
{
    <h2>Expenditure details</h2>
    <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");}
}

CodePudding user response:

By default, properties are not bound for HTTP GET requests. In scenarios where you do want properties bound to data from GET requests, set the SupportsGet property to true.

Reference - Microsoft docs.

  • Related