在传递的对象上获取 ModelState.IsValid == false

Getting ModelState.IsValid == false on passed objects

我收到一条错误消息“需要计算字段”

这是我的支出模型:

public class ExpendituresModel : PageModel
    {
        private readonly ISqlData _db;

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

        [BindProperty]
        public Project Project { get; set; }
        public List<Expenditure> Expenditures { get; set; } = null!;

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

      //  public Calculations Calculations { get; set; } = new Calculations();

        public int ProjectId { get; set; }
        

        public async Task OnGetAsync(int projectId)
        {
            Project = await _db.GetProjectDataAsync(projectId);

            //Expenditures = await _db.GetExpendituresByProjectIdAsync(projectId);

            var calculations = await _db.GetCalculationByProjectIdAsync(projectId);

            WipCommentCalculation = new WipCommentCalculation
            {
                Calculations = calculations,
                Comments = null,
                ProjectId = projectId
            };
        }

        public IActionResult OnPost()
        {
            if (ModelState.IsValid ==false)
            {
                return RedirectToPage("./Expenditures");
            }

            return RedirectToPage("./Expenditures");
        }
    }

这是它的视图:

@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.ExpendituresModel
@{
    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">
            <thead>
            <tr>
                <th></th>
                <th>Billable</th>
                <th>Unbilled</th>
                <th>Billing Hold</th>
                <th>Project Accountant explanation</th>
                <th>Project Accounting Manager comments</th>
            </tr>
            </thead>
            <tbody>
            
            <tr>
                <div class="mb-3">
                    <td>Labor</td>
                    <td>@($"{Model.WipCommentCalculation.Calculations.BillableLabor:0,0.00}")</td>
                    <td>@($"{Model.WipCommentCalculation.Calculations.UnbilledLabor:0,0.00}")</td>
                    <td>@($"{Model.WipCommentCalculation.Calculations.BillingHoldLabor:0,0.00}")</td>
                    @*<td><textarea class="form-control" asp-for="WipCommentCalculation.Comments.PALaborComment" id="exampleFormControlTextarea1" rows="1"></textarea></td>*@

                    <td><input asp-for="WipCommentCalculation.Comments.PALaborComment"/></td>
                    <td><input asp-for="WipCommentCalculation.Comments.PMLaborComment"/></td>
                </div>
            </tr>
            
            <tr>
                <td>Non-Labor</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.BillableNonLabor:0,0.00}")</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.UnbilledNonLabor:0,0.00}")</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.BillingHoldNonLabor:0,0.00}")</td>
                <td><input asp-for="WipCommentCalculation.Comments.PANonLaborComment"/></td>
                <td><input asp-for="WipCommentCalculation.Comments.PMNonLaborComment"/></td>
            </tr>
            <tr>
                <td>SubContractor</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.BillableSubcontractor:0,0.00}")</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.UnbilledSubcontractor:0,0.00}")</td>
                <td>@($"{Model.WipCommentCalculation.Calculations.BillingHoldSubcontractor:0,0.00}")</td>
                <td><input asp-for="WipCommentCalculation.Comments.PASubContractorComment"/></td>
                <td><input asp-for="WipCommentCalculation.Comments.PMSubcontractorComment"/></td>
            </tr>
            <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>
            </tbody>
        </table>
    </div>
    <div class="form-control">
        <input type="submit" value="Save" class="btn btn-primary"/>
    </div>
</form>
@section Scripts
{
    @{ await Html.RenderPartialAsync("Shared/_ValidationScriptsPartial");}
}

我不确定是否应该以不同的方式设置对象,

我进行了计算,但在调用 OnPost 时在对象中显示为 null

我想稍后将此对象与评论一起保存在数据库中,但来自表单的模型状态无效。

不知道怎么解释好。例如,我在我这边复制了您的代码并创建了一个模型 class,如下所示:

namespace WebApp.Model
{
    public class WipCommentCalculation
    {
        public int ProjectId { get; set; }
        public Calculations Calculations { get; set; }
    }

    public class Calculations {
        public decimal BillableLabor { get; set; }
        public decimal UnbilledLabor { get; set; }
        public decimal BillingHoldLabor { get; set; }
        public decimal UnbilledSubcontractor { get; set; }
        public decimal BillingHoldSubcontractor { get; set; }
        public decimal BillableNonLabor { get; set; }
        public decimal UnbilledNonLabor { get; set; }
        public decimal BillingHoldNonLabor { get; set; }
        public decimal BillableSubcontractor { get; set; }
    }

    public class Project {
        public string FiscalMonth { get; set; }
        public string PtdBilled { get; set; }
    }
}

然后当我把你的代码放在我的视图中时,我也会让它无效,但是如果我像这样使用表单dom,那么它就可以有效:

<form method="post">
   <div>
        <div>
            <label for="BillableLabor">BillableLabor:</label>
            <input type="text" asp-for="@Model.WipCommentCalculation.Calculations.BillableLabor" value=""  />
        </div>
        <div>
            <label for="UnbilledLabor">UnbilledLabor:</label>
            <input type="text" asp-for="@Model.WipCommentCalculation.Calculations.UnbilledLabor" value=""  />
        </div>
    </div>
    <div class="form-control">
        <input type="submit" value="Save" class="btn btn-primary"/>
    </div>
</form>

我想你使用了ModelState.IsValid那么你应该遵守提交规则。对元素使用标签助手 asp-for="@Model.WipCommentCalculation.Calculations.BillableLabor"