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;

namespace Website.Pages.Support.Samples.RazorPages.Calculations
{
    public partial class SimpleLoanCalculatorModel : PageModel
    {
        [BindProperty]
        public string LoanAmount { get; set; }

        [BindProperty]
        public string InterestRate { get; set; }

        [BindProperty]
        public string Periods { get; set; }

        public string PaymentAmount { get; set; }



        public void OnGet()
        {
            LoanAmount = "150000";
            InterestRate = "6.25%";
            Periods = "360";
        }

        public void OnPost()
        {
            // 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.Formula = Periods;

            // Display the result
            PaymentAmount = workbook.Names["payment"].RefersToRange.Text;
        }
    }
}
@page
@model SimpleLoanCalculatorModel
@{ Layout = "_SamplesLayout";}

<div class="row">
  <div class="col-sm-12 col-md-6 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" />
          </div>

          <div class="mb-2">
            <label asp-for="InterestRate" class="form-label">Interest Rate (rate)</label>
            <input asp-for="InterestRate" class="form-control" />
          </div>

          <div class="mb-2">
            <label asp-for="Periods" class="form-label">Total # of Periods (Nper)</label>
            <input asp-for="Periods" class="form-control" />
          </div>

          <div class="text-end">
            <button class="btn btn-primary">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 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>