问题描述
当贷款金额、摊销和利率被填满时,贷款摊销时间表将使用 _displayamortizationSchedule 部分视图显示。但是,当字段留空时,我不确定如何在文本框下方显示错误消息,而不在 UpdateTargetId="displayResultDiv" 下显示整个视图。
模型:LoanPaymentInputs.cs
public class LoanPaymentInputs
{
[display(Name = "Loan Amount: ")]
[required(ErrorMessage = "Amount is required.")]
public double LoanAmount { get; set; }
[display(Name = "amortization: ")]
[required(ErrorMessage = "amortization in months is required.")]
public int amortization { get; set; }
[display(Name = "Interest Rate: ")]
[required(ErrorMessage = "Rate is required. ")]
public double InterestRate { get; set; }
}
控制器:CalculatorsController.cs
public ActionResult PrincipalAndInterest()
{
return View();
}
public PartialViewResult displayamortizationTable()
{
Tuple<double,int,double> amort = new Tuple<double,double>(((Tuple<double,double>)TempData["paymentInputsTuple"]).Item1,((Tuple<double,double>)TempData["paymentInputsTuple"]).Item2,double>)TempData["paymentInputsTuple"]).Item3);
amortizationTable amortizationTable = new amortizationTable(amort.Item1,amort.Item2,amort.Item3);
LoanamortizationSchedule loan = new LoanamortizationSchedule
{
amortTable = amortizationTable.PrintLoanamortizationPrincipalandInterest(),amortSummary = amortizationTable.FiveYearPrincipalAndInterest(),LoanAmount = amort.Item1,amortization = amort.Item2,InterestRate = amort.Item3 / 100,TotalInterest = amortizationTable.TotalInterest
};
return PartialView("_displayamortizationSchedule",loan);
}
[HttpPost]
public ActionResult displayamortizationTable(LoanPaymentInputs paymentInputs)
{
if (!ModelState.IsValid)
{
ViewBag.success = false;
return View("PrincipalAndInterest",paymentInputs);
}
TempData["paymentInputsTuple"] = new Tuple<double,double>(paymentInputs.LoanAmount,paymentInputs.amortization,paymentInputs.InterestRate);
return RedirectToAction("displayamortizationTable","Calculators",new { Area = "ToolBox" });
}
查看:PrincipalAndInterest.cshtml
<div class="card">
<div class="card-body">
<div class="row">
<div class="col">
@using (Ajax.BeginForm("displayamortizationTable",new { area = "ToolBox" },new AjaxOptions() { HttpMethod = "POST",UpdateTargetId = "displayResultDiv" },new { id = "myForm" }))
{
<div class="row">
<div class="col">
@Html.LabelFor(x => x.LoanAmount,new { @class = "font-weight-bold" })
</div>
<div class="col">
@Html.LabelFor(x => x.amortization,new { @class = "font-weight-bold" })
</div>
<div class="col">
@Html.LabelFor(x => x.InterestRate,new { @class = "font-weight-bold" })
</div>
<div class="col">
</div>
</div>
<div class="row">
<div class="col">
@Html.TextBoxFor(x => x.LoanAmount,new { @class = "form-control",@placeholder = "e.g. 25,000",id = "currency" })
</div>
<div class="col">
@Html.TextBoxFor(x => x.amortization,new
{
@class = "form-control",@placeholder = "e.g. 60"
})
</div>
<div class="col">
@Html.TextBoxFor(x => x.InterestRate,@placeholder = "e.g. 4.25"
})
</div>
<div class="col">
<input type="submit" value="Calculate" class="btn btn-outline-primary" />
</div>
</div>
<div class="row">
<div class="col">
@Html.ValidationMessageFor(x => x.LoanAmount,"",new { @class = "text-danger" })
</div>
<div class="col">
@Html.ValidationMessageFor(x => x.amortization,new { @class = "text-danger" })
</div>
<div class="col">
@Html.ValidationMessageFor(x => x.InterestRate,new { @class = "text-danger" })
</div>
<div class="col">
</div>
</div>
}
</div>
</div>
</div>
</div>
<div>
<br />
</div>
<div id="displayResultDiv">
</div>
部分视图:_displayamortizationSchedule.cshtml
<div>
<div class="card">
<div class="card-body">
<h5>Summary</h5>
<p>Loan: @string.Format("{0:C}",Model.LoanAmount) | amortization: @Model.amortization months | Rate: @string.Format("{0:P}",Model.InterestRate) </p>
<p>Total Interest Paid: @string.Format("{0:C}",Model.TotalInterest)</p>
<table class="table table-sm">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Year One</th>
<th scope="col">Year Two</th>
<th scope="col">Year Three</th>
<th scope="col">Year Four</th>
<th scope="col">Year Five</th>
</tr>
</thead>
@foreach (var item in Model.amortSummary)
{
<tbody>
<tr>
<th scope="row">Principal</th>
<td>@string.Format("{0:C}",decimal.Round((decimal)item.YearOneAnnualPrincipal,2,MidpointRounding.AwayFromZero))</td>
<td>@string.Format("{0:C}",decimal.Round((decimal)item.YearTwoAnnualPrincipal,decimal.Round((decimal)item.YearThreeAnnualPrincipal,decimal.Round((decimal)item.YearFourAnnualPrincipal,decimal.Round((decimal)item.YearFiveAnnualPrincipal,MidpointRounding.AwayFromZero))</td>
</tr>
<tr>
<th scope="row">Interest</th>
<td>@string.Format("{0:C}",decimal.Round((decimal)item.YearOneAnnualInterest,decimal.Round((decimal)item.YearTwoAnnualInterest,decimal.Round((decimal)item.YearThreeAnnualInterest,decimal.Round((decimal)item.YearFourAnnualInterest,decimal.Round((decimal)item.YearFiveAnnualInterest,MidpointRounding.AwayFromZero))</td>
</tr>
<tr>
<th scope="row">Total Debt Service</th>
<td>@string.Format("{0:C}",decimal.Round((decimal)item.YearOneAnnualDebtService,decimal.Round((decimal)item.YearTwoAnnualDebtService,decimal.Round((decimal)item.YearThreeAnnualDebtService,decimal.Round((decimal)item.YearFourAnnualDebtService,decimal.Round((decimal)item.YearFiveAnnualDebtService,MidpointRounding.AwayFromZero))</td>
</tr>
</tbody>
}
</table>
</div>
</div>
<div>
<br />
</div>
<div class="card">
<blockquote class="blockquote mb-0">
<div class="card-body">
<table class="tableFixHead table table-sm table-hover">
<thead>
<tr>
<th scope="col">Months</th>
<th scope="col">Beginning Balance</th>
<th scope="col">Payment</th>
<th scope="col">Interest</th>
<th scope="col">Principal</th>
<th scope="col">Ending Balance</th>
</tr>
</thead>
@foreach (keyvaluePair<int,amortizationTable> item in Model.amortTable)
{
<tbody>
<tr>
<th scope="row">@item.Key.ToString()</th>
<td class="border border-left">@string.Format("{0:C}",decimal.Round((decimal)item.Value.BeginningBalance,MidpointRounding.AwayFromZero))</td>
<td class="border border-left">@string.Format("{0:C}",decimal.Round((decimal)item.Value.MonthlyPayment,decimal.Round((decimal)item.Value.MonthlyInterest,decimal.Round((decimal)item.Value.MonthlyPrincipal,MidpointRounding.AwayFromZero))</td>
<td>@string.Format("{0:C}",decimal.Round((decimal)item.Value.EndingBalance,MidpointRounding.AwayFromZero))</td>
</tr>
</tbody>
}
</table>
</div>
</blockquote>
</div>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)