Live Razor Page Samples
Simple Loan Calculator
This sample demonstrates calculating a loan payment based on input from a web form. By using this design pattern, you can easily deploy complex numeric, financial and statistics calculators which call on the full power of SpreadsheetGear's Microsoft Excel compatible calculation capabilities.
Supporting Files
The following file is utilized by this sample:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel.DataAnnotations;
namespace Website.Pages.Support.Samples.RazorPages.Calculations
{
public partial class SimpleLoanCalculatorModel : PageModel
{
[BindProperty]
[StringLength(25)]
[Display(Name = "Loan Amount")]
public string LoanAmount { get; set; }
[BindProperty]
[StringLength(25)]
[Display(Name = "Interest Rate")]
public string InterestRate { get; set; }
[BindProperty]
[Range(1, 360)]
[Display(Name = "Total # of Periods")]
public int Periods { get; set; }
public string PaymentAmount { get; set; }
public void OnGet()
{
LoanAmount = "150000";
InterestRate = "6.25%";
Periods = 360;
}
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
SetErrorMessage("Error occurred while running sample. Please correct the errors shown below.");
return Page();
}
// Open the workbook
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook("files/simpleloancalc.xls");
// Get IRange for cells from defined names.
SpreadsheetGear.IRange pv = workbook.Names["pv"].RefersToRange;
SpreadsheetGear.IRange rate = workbook.Names["rate"].RefersToRange;
SpreadsheetGear.IRange nper = workbook.Names["nper"].RefersToRange;
// Copy the form values to the worksheet.
pv.Formula = LoanAmount;
rate.Formula = InterestRate;
nper.Value = Periods;
// Display the result
PaymentAmount = workbook.Names["payment"].RefersToRange.Text;
return Page();
}
}
}
@page
@model SimpleLoanCalculatorModel
@{ Layout = "_SamplesLayout";}
<div class="row justify-content-center">
<div class="col-sm-12 col-md-6 col-lg-5 mb-3">
<div class="card">
<h2 class="card-header">Loan Inputs</h2>
<div class="card-body">
<!-- Input Form -->
<form asp-page="SimpleLoanCalculator" method="post">
<div class="mb-2">
<label asp-for="LoanAmount" class="form-label">Loan Amount (pv)</label>
<input asp-for="LoanAmount" class="form-control" />
<span asp-validation-for="LoanAmount" class="form-control-feedback"></span>
</div>
<div class="mb-2">
<label asp-for="InterestRate" class="form-label">Interest Rate (rate)</label>
<input asp-for="InterestRate" class="form-control" />
<span asp-validation-for="InterestRate" class="form-control-feedback"></span>
</div>
<div class="mb-2">
<label asp-for="Periods" class="form-label">Total # of Periods (Nper)</label>
<input asp-for="Periods" class="form-control" class="form-control" style="width: 150px;" min="1" max="360" />
<span asp-validation-for="Periods" class="form-control-feedback"></span>
<div class="form-text">Periods must be between 1 and 360.</div>
</div>
<div class="text-end">
<button class="btn btn-primary"><i class="fa-fw fas fa-play-circle"></i> Calculate</button>
</div>
</form>
<!-- Output Result Here -->
<div class="m-4" style="display:@(Model.PaymentAmount != null ? "block" : "none")">
<div class="alert alert-info">
Calculated Payment per Period is: <b>@Model.PaymentAmount</b>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-12 col-md-5 col-lg-3 mb-3">
<div class="card">
<h4 class="card-header"><i class="fas fa-info-circle"></i> Notice These Features</h4>
<div class="card-body">
<ul>
<li>SpreadsheetGear parses inputs, including currency, percent, date, time and fractions.</li>
<li>Allows simple or complex formulas. Enter "=1% * 12" for Interest Rate (don't include the quotes).</li>
<li>Returns nicely formatted results, including all Excel custom formatting options.</li>
<li>Uses Defined Names to access cells (see the source code and workbook).</li>
</ul>
</div>
</div>
</div>
</div>