为具有不同输入文本的表单设置模型的正确方法
Correct way to setup a model for a form with different input text
我正在尝试为表单设置模型,但我很难理解应该如何设置模型来捕获我想要的正确信息。
(信息:我是使用 RazorPages 的用户 webApp)
我想实现这一点,每个“评论”都是一个不同的文本框,用户可以在其中插入文本:
稍后我想将所有这些保存到 sql 服务器数据库中,以便以后检索其他用户添加的评论。
我在支出模型文件中创建了这个:
public async Task OnGetAsync(int projectId)
{
Project = await _db.GetProjectDataAsync(projectId);
var calculations = await _db.GetCalculationByProjectIdAsync(projectId);
WipCommentCalculation = new WipCommentCalculation
{
Calculations = new Calculations
{
BillableLabor = calculations.BillableLabor,
BillableNonLabor = calculations.BillableNonLabor,
BillableSubcontractor = calculations.BillableSubcontractor,
UnbilledLabor = calculations.UnbilledLabor,
UnbilledNonLabor = calculations.UnbilledNonLabor,
UnbilledSubcontractor = calculations.UnbilledSubcontractor,
BillingHoldLabor = calculations.BillingHoldLabor,
BillingHoldNonLabor = calculations.BillingHoldNonLabor,
BillingHoldSubcontractor = calculations.BillingHoldSubcontractor
},
Comments = null,
ProjectId = projectId
};
}
我在视图中有这个:
<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 Manager Accountant comments</th>
</tr>
</thead>
<tbody>
<tr>
<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><input asp-for="WipCommentCalculation.Comments.PALaborComment"/></td>
<td><input asp-for="WipCommentCalculation.Comments.PMLaborComment"/></td>
</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>
</form>
这些是模型:
public class WipCommentCalculation
{
public Calculations Calculations{ get; set; }
public CommentModel Comments { get; set; }
public int ProjectId{ get; set; }
}
public class Calculations
{
public decimal BillableLabor { get; set; }
public decimal BillableNonLabor { get; set; }
public decimal BillableSubcontractor { get; set; }
public decimal UnbilledLabor { get; set; }
public decimal UnbilledNonLabor { get; set; }
public decimal UnbilledSubcontractor { get; set; }
public decimal BillingHoldLabor { get; set; }
public decimal BillingHoldNonLabor { get; set; }
public decimal BillingHoldSubcontractor { get; set; }
}
public class CommentModel
{
public string PALaborComment { get; set; } = null!;
public string PANonLaborComment { get; set; } = null!;
public string PASubContractorComment { get; set; } = null!;
public string PMLaborComment { get; set; } = null!;
public string PMNonLaborComment { get; set; } = null!;
public string PMSubcontractorComment { get; set; } = null!;
}
但我不知道我设置的方式是否正确,我可以对此进行批评和更好的方法吗?如果需要,我还可以添加 github 存储库。
谢谢!
我认为表示 table 的最简单方法是使用具有对象列表的模型,其中对象具有 table 的所有属性。
public class TableModel
{
List<Row> rows { get; set; }
...
...
}
此外,TableModel 还可以具有您可能需要的其他属性。
通过这种方法,您可以拥有一个可以适应无限行数并且可以轻松更新的动态模型。
我正在尝试为表单设置模型,但我很难理解应该如何设置模型来捕获我想要的正确信息。 (信息:我是使用 RazorPages 的用户 webApp)
我想实现这一点,每个“评论”都是一个不同的文本框,用户可以在其中插入文本:
稍后我想将所有这些保存到 sql 服务器数据库中,以便以后检索其他用户添加的评论。
我在支出模型文件中创建了这个:
public async Task OnGetAsync(int projectId)
{
Project = await _db.GetProjectDataAsync(projectId);
var calculations = await _db.GetCalculationByProjectIdAsync(projectId);
WipCommentCalculation = new WipCommentCalculation
{
Calculations = new Calculations
{
BillableLabor = calculations.BillableLabor,
BillableNonLabor = calculations.BillableNonLabor,
BillableSubcontractor = calculations.BillableSubcontractor,
UnbilledLabor = calculations.UnbilledLabor,
UnbilledNonLabor = calculations.UnbilledNonLabor,
UnbilledSubcontractor = calculations.UnbilledSubcontractor,
BillingHoldLabor = calculations.BillingHoldLabor,
BillingHoldNonLabor = calculations.BillingHoldNonLabor,
BillingHoldSubcontractor = calculations.BillingHoldSubcontractor
},
Comments = null,
ProjectId = projectId
};
}
我在视图中有这个:
<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 Manager Accountant comments</th>
</tr>
</thead>
<tbody>
<tr>
<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><input asp-for="WipCommentCalculation.Comments.PALaborComment"/></td>
<td><input asp-for="WipCommentCalculation.Comments.PMLaborComment"/></td>
</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>
</form>
这些是模型:
public class WipCommentCalculation
{
public Calculations Calculations{ get; set; }
public CommentModel Comments { get; set; }
public int ProjectId{ get; set; }
}
public class Calculations
{
public decimal BillableLabor { get; set; }
public decimal BillableNonLabor { get; set; }
public decimal BillableSubcontractor { get; set; }
public decimal UnbilledLabor { get; set; }
public decimal UnbilledNonLabor { get; set; }
public decimal UnbilledSubcontractor { get; set; }
public decimal BillingHoldLabor { get; set; }
public decimal BillingHoldNonLabor { get; set; }
public decimal BillingHoldSubcontractor { get; set; }
}
public class CommentModel
{
public string PALaborComment { get; set; } = null!;
public string PANonLaborComment { get; set; } = null!;
public string PASubContractorComment { get; set; } = null!;
public string PMLaborComment { get; set; } = null!;
public string PMNonLaborComment { get; set; } = null!;
public string PMSubcontractorComment { get; set; } = null!;
}
但我不知道我设置的方式是否正确,我可以对此进行批评和更好的方法吗?如果需要,我还可以添加 github 存储库。
谢谢!
我认为表示 table 的最简单方法是使用具有对象列表的模型,其中对象具有 table 的所有属性。
public class TableModel
{
List<Row> rows { get; set; }
...
...
}
此外,TableModel 还可以具有您可能需要的其他属性。
通过这种方法,您可以拥有一个可以适应无限行数并且可以轻松更新的动态模型。