Introduction

ASP.NET provides various options to create web-based applications. The latest one is ASP.NET Core with razor pages. To experiment with it, we will create an application named Watts' A Loan. It is for a small company that issues various types of loans.

Practical LearningPractical Learning: Introducing the Application

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. On the Create a New Project dialog box, click ASP.NET Core Web App (Razor Pages)
  4. Click Next
  5. Change the project Name to WattsALoan1
  6. Click Next
  7. In the Additional Information wizard page, in the Framework combo box, select the highest version (.NET 9.0 (Standard Term Support))
  8. Click Create
  9. In the Solution Explorer, right-click wwwroot -> Add -> New Folder
  10. Type Images
  11. Copy and paste the following pictures to the images folder:

    Background Picture
    Watts' A Loan Watts' A Loan Watts' A Loan
  12. In the Solution Explorer, below wwwroot, expand css and double-click Site.css
  13. Change the document as follows:
    body {
        background-color: #370000;
    }
    
    .creame            { color:              wheat;            }
    .yellow            { color:              orange;           }
    .containment       { margin:             auto; 
                         width:              400px;            }
    .top-menu-holder   { width:              800px;
                         margin:             auto;             }
    .form-horizontal   { width:              400px;            }
    .maint-title       { color:              sandybrown;
                         font-weight:        bold;             }
    .texting           { color:              bisque;           }
    .tbl-row           { color:              antiquewhite;     }
    
    .tbl-head          { color:              yellow;
                         font-weight:        bold;             }
    .form-control      { border:             solid 1px maroon; }
    
    .jumbotron         { background-color:   #370000;          }
    .jumbotron h1      { color:              yellow;
                         font-weight:        600;
                         text-shadow:        5px 5px black;    }
    
    .menu-box          { color:              maroon;
                         border-radius:      5px;
                         background-color:   burlywood;
                         font-family:        Garamond, 'Times New Roman', Georgia, serif; }
    .menu-box ul       { list-style-type:    none;             }
    
    .border-bottom     { border-bottom:      5px solid yellow !important; }
    
    .btn-primary       { color:              #800000;
                         background-color:   wheat;
                         border-color:       #000000;         }
    .btn-primary:hover { color:              #fff;
                         background-color:   orange;
                         border-color:       #800000;         }
    
    .bg-white          { background-color:   #800000 !important; }
    
    a.text-dark:hover  { color:              #ffd800 !important; }
    a.text-dark:focus  { color:              antiquewhite !important; }
    .wal-lead          { color:              yellow;
                         font-family:        Georgia, Garamond, 'Times New Roman', serif; }
    a.wal-lead:hover   { color:              greenyellow;        }
    a.wal-lead:focus   { color:              orange;             }
    .wal-leader        { color:              orange;             }
    a.wal-leader:hover { color:              yellow;             }
    a.wal-leader:focus { color:              aqua;               }
    
    .text-dark         { color:              antiquewhite !important;
                         font-family:        Garamond, 'Times New Roman', Georgia, serif; }
    .common-font       { font-family:        Georgia, Garamond, 'Times New Roman', serif; }
    
    .navbar-light .navbar-brand              {
                         color:              antiquewhite;
                         font-family:        Garamond, 'Times New Roman', Georgia, serif; }
    .navbar-light .navbar-brand:hover {
                         color:              yellow       }
    .navbar-light .navbar-brand:focus {
                         color:              aliceblue;   }
    .navbar-light .navbar-brand {
                         color:              antiquewhite;
                         font-family:        Garamond, 'Times New Roman', Georgia, serif; }
  14. In the Solution Explorer, right-click WattsALoan1 -> Add -> New Folder
  15. Type Models as the name of the folder, and press Enter
  16. In the Solution Explorer, right-click Models -> Add -> Class...
  17. Type LoanType as the name of the class
  18. Click Add
  19. In the Solution Explorer, right-click Models -> Add -> Class...
  20. Type Payment as the name of the class
  21. Click Add
  22. In the Solution Explorer, right-click Models -> Add -> Class...
  23. Type LoanContract as the name of the class
  24. Click Add

Employees

The employees are the people who process transactions and perform other housekeeping chores for a company. To keep track of them, we will create a class and the necessary forms.

Practical LearningPractical Learning: Creating Employees

  1. In the Solution Explorer, right-click Models -> Add -> Class...
  2. Type Employee as the name of the class
  3. Click Add
  4. Change the document as follows:
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    
    namespace WattsALoan2.Models
    {
        public class Employee
        {
            public Employee()
            {
                LoansContracts = new HashSet<LoanContract>();
                Payments = new HashSet<Payment>();
            }
    
            [Display(Name = "Employee ID")]
            public int EmployeeID { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Employee #")]
            public string EmployeeNumber { get; set; }
    
            [StringLength(20)]
            [Display(Name = "First Name")]
            public string FirstName { get; set; }
    
            [StringLength(20)]
            [Display(Name = "Last Name")]
            public string LastName { get; set; }
    
            [StringLength(50)]
            [Display(Name = "Employee Title")]
            public string EmploymentTitle { get; set; }
    
            public string Identification
            {
                get
                {
                    return EmployeeNumber + " - " + FirstName + " " + LastName + " (" + EmploymentTitle + ")";
                }
            }
    
            public virtual ICollection<Payment> Payments { get; set; }
            public virtual ICollection<LoanContract> LoansContracts { get; set; }
        }
    }
  5. In the Solution Explorer, under Models, double-click LoanType
  6. Change the class as follows:
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    
    namespace WattsALoan2.Models
    {
        public class LoanType
        {
            public LoanType()
            {
                LoansContracts = new HashSet<LoanContract>();
            }
    
            [Display(Name = "LoanType ID")]
            public int LoanTypeID { get; set; }
    
            [StringLength(25)]
            [Display(Name = "Loan Name")]
            public string LoanName { get; set; }
    
            [MinLength(2)]
            [MaxLength(1024)]
            public string Description { get; set; }
    
            public virtual ICollection<LoanContract> LoansContracts { get; set; }
        }
    }
  7. In the Solution Explorer, under Models, double-click LoanContract to access it
  8. Change the document as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    
    namespace WattsALoan2.Models
    {
        public class LoanContract
        {
            public LoanContract()
            {
                Payments = new HashSet<Payment>();
            }
    
            [Display(Name = "Loan Contract ID")]
            public int LoanContractID { get; set; }
    
            [Display(Name = "Loan #")]
            public int LoanNumber { get; set; }
    
            [DataType(DataType.Date)]
            [Display(Name = "Date Allocated")]
            public DateTime DateAllocated { get; set; }
    
            [Display(Name = "Processed By")]
            public int? EmployeeID { get; set; }
    
            [Display(Name = "First Name")]
            public string CustomerFirstName { get; set; }
    
            [Display(Name = "Last Name")]
            public string CustomerLastName { get; set; }
    
            [Display(Name = "Loan Type")]
            public int? LoanTypeID { get; set; }
    
            [Display(Name = "Loan Amount")]
            public decimal LoanAmount { get; set; }
    
            [Display(Name = "Interest Rate")]
            public decimal InterestRate { get; set; }
    
            public int Periods { get; set; }
    
            [Display(Name = "Interest Amount")]
            public decimal InterestAmount { get; set; }
    
            [Display(Name = "Future Value")]
            public decimal FutureValue { get; set; }
    
            [Display(Name = "Monthly Payment")]
            public decimal MonthlyPayment { get; set; }
    
            [DataType(DataType.Date)]
            [Display(Name = "Payment Start Date")]
            public DateTime PaymentStartDate { get; set; }
    
            [Display(Name = "Current Balance")]
            public decimal CurrentBalance { get; set; }
    
            public string LoanDetails
            {
                get
                {
                    return "Loan # " + LoanNumber.ToString() + " in the amount of " +
                            LoanAmount.ToString("C") + " for " + CustomerFirstName + " " +
                           CustomerLastName + " paid at " + MonthlyPayment.ToString("F") +
                           "/month; current balance: $" + CurrentBalance.ToString("C");
                }
            }
    
            public virtual Employee Employee { get; set; }
            public virtual LoanType LoanType { get; set; }
    
            public virtual ICollection<Payment> Payments { get; set; }
        }
    }
  9. In the Solution Explorer, under Models, double-click Payment
  10. Change the document as follows:
    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace WattsALoan2.Models
    {
        public class Payment
        {
            [Display(Name = "Payment ID")]
            public int PaymentID { get; set; }
            [Display(Name = "Receipt #")]
            public int ReceiptNumber { get; set; }
            [DataType(DataType.Date)]
            [Display(Name = "Payment Date")]
            public DateTime PaymentDate { get; set; }
            [Display(Name = "Processed By")]
            public int? EmployeeID { get; set; }
            [Display(Name = "Loan Contract")]
            public int? LoanContractID { get; set; }
            [Display(Name = "Payment Amount")]
            public decimal PaymentAmount { get; set; }
    
            public virtual Employee Employee { get; set; }
            public virtual LoanContract LoanContract { get; set; }
        }
    }
  11. If the Solution Explorer, right-click Pages -> Add -> New Folder
  12. Type Employees as the name of the folder and press Enter
  13. In the Solution Explorer, under Pages, right-click Employees -> Add -> New Scaffolded Item...
  14. In the left list of the Add New Scaffolded Item dialog box Explorer, click Razor Pages and, in the middle list, click Razor Pages using Entity Framework (CRUD)
  15. Click Add
  16. Click the arrow of the Model Class combo box

    Watts A Loan - Add Razor Pages Using Entity Framework (CRUD)

    Select Employee (WattsALoan1.Models)
  17. Click the + button of the Data Context Class text box to create a class
  18. Accept the content of the Add Data Context Type dialog box

    Watts A Loan - Add Razor Pages Using Entity Framework (CRUD)

    Click Add

    Watts A Loan - Add Razor Pages Using Entity Framework (CRUD)

  19. On the Add Razor Pages Using Entity Framework (CRUD) dialog box, click Add
  20. In the Solution Explorer, under Pages, expand Employees and double-click Create
  21. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Employees.CreateModel
    
    @{
        ViewData["Title"] = "Create Employee";
    }
    
    <h1 class="bold creme common-font text-center">Employment Application</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <label asp-for="Employee.EmployeeNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.EmployeeNumber" class="form-control" />
                <span asp-validation-for="Employee.EmployeeNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.FirstName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.FirstName" class="form-control" />
                <span asp-validation-for="Employee.FirstName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.LastName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.LastName" class="form-control" />
                <span asp-validation-for="Employee.LastName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.EmploymentTitle" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.EmploymentTitle" class="form-control" />
                <span asp-validation-for="Employee.EmploymentTitle" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="Index" class="wal-lead col-md-5">Display Employees</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Save Employee Record" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  22. In the Solution Explorer, under Pages and under Employees, double-click Delete.cshtml
  23. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Employees.DeleteModel
    
    @{
        ViewData["Title"] = "Delete Employee";
    }
    
    <h1 class="bold creme common-font text-center">Delete Employee Record</h1>
    
    <hr color="yellow" />
    
    <h3 class="creme text-center common-font">Are you sure you want to delete this employee record?</h3>
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmployeeID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmployeeID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmployeeNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmployeeNumber)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.FirstName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.FirstName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.LastName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.LastName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmploymentTitle)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmploymentTitle)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <form method="post">
            <a asp-page="./Index" class="text-center wal-lead">Staff Members</a> ::
            <input type="hidden" asp-for="Employee.EmployeeID" />
            <input type="submit" value="Delete Employee" class="btn btn-primary" />
        </form>
    </div>
  24. In the Solution Explorer, under Pages and under Employees, double-click Details.cshtml
  25. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Employees.DetailsModel
    
    @{
        ViewData["Title"] = "Employee Details";
    }
    
    <h1 class="bold creme common-font text-center">Employee Details</h1>
    
    <hr color="yellow" />
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmployeeID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmployeeID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmployeeNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmployeeNumber)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.FirstName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.FirstName)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.LastName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.LastName)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Employee.EmploymentTitle)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Employee.EmploymentTitle)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div>
            <a asp-page="./Index" class="text-center wal-lead">Employees</a> ::
            <a asp-page="./Edit" asp-route-id="@Model.Employee.EmployeeID" class="wal-lead">Edit Employee</a>
        </div>
    </div>
  26. In the Solution Explorer, under Pages and under Employees, double-click Edit.cshtml
  27. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Employees.EditModel
    
    @{
        ViewData["Title"] = "Edit Employee";
    }
    
    <h1 class="bold creme common-font text-center">Edit/Update Employee</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <input type="hidden" asp-for="Employee.EmployeeID" />
            <label asp-for="Employee.EmployeeNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.EmployeeNumber" class="form-control" />
                <span asp-validation-for="Employee.EmployeeNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.FirstName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.FirstName" class="form-control" />
                <span asp-validation-for="Employee.FirstName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.LastName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.LastName" class="form-control" />
                <span asp-validation-for="Employee.LastName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Employee.EmploymentTitle" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Employee.EmploymentTitle" class="form-control" />
                <span asp-validation-for="Employee.EmploymentTitle" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="./Index" class="wal-lead col-md-5">Display Employees</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Save the Update" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  28. In the Solution Explorer, under Pages and under Employees, double-click Index.cshtml
  29. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Employees.IndexModel
    
    @{
        ViewData["Title"] = "Employees";
    }
    
    <h1 class="bold maint-title common-font text-center">Employees</h1>
    
    <table class="table common-font">
        <thead>
            <tr class="tbl-head">
                <th class="text-center">@Html.DisplayNameFor(model => model.Employee[0].EmployeeID)</th>
                <th>@Html.DisplayNameFor(model => model.Employee[0].EmployeeNumber)</th>
                <th>@Html.DisplayNameFor(model => model.Employee[0].FirstName)</th>
                <th>@Html.DisplayNameFor(model => model.Employee[0].LastName)</th>
                <th>@Html.DisplayNameFor(model => model.Employee[0].EmploymentTitle)</th>
                <th><a asp-page="Create" class="wal-leader">Hire Employee</a></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Employee)
            {
                <tr class="tbl-row">
                    <td class="text-center">@Html.DisplayFor(modelItem => item.EmployeeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.EmployeeNumber)</td>
                    <td>@Html.DisplayFor(modelItem => item.FirstName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LastName)</td>
                    <td>@Html.DisplayFor(modelItem => item.EmploymentTitle)</td>
                    <td>
                        <a asp-page="./Edit"    asp-route-id="@item.EmployeeID" class="wal-lead">Edit</a> ::
                        <a asp-page="./Details" asp-route-id="@item.EmployeeID" class="wal-lead">Details</a> ::
                        <a asp-page="./Delete"  asp-route-id="@item.EmployeeID" class="wal-lead">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

Loans Types

A financial instituation, such as the one for our application, provides various types of loans. Because companies vary, instead of providing a fixed list of types of loans, we will let the employees of the company create the names of loans.

Practical LearningPractical Learning: Managing Loans Types

  1. In the Solution Explorer, right-click Pages -> Add -> New Folder
  2. Type LoansTypes as the name of the folder, and press Enter
  3. In the Solution Explorer, under Pages, right-click LoansTypes -> Add -> New Scaffolded Item...
  4. In the middle list of the Add New Scaffolded Item dialog box Explorer, make sure Razor Pages using Entity Framework (CRUD) is selected.
    Click Add
  5. Click the arrow of the Model Class combo box and select LoanType (WattsALoan1.Models)
  6. Accept the content of the Add Data Context Type dialog box and click Add
  7. In the Solution Explorer, under Pages, expand LoansTypes and double-click Create
  8. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansTypes.CreateModel
    
    @{
        ViewData["Title"] = "Create Loan Type";
    }
    
    <h1 class="bold creme common-font text-center">Create a Loan Type</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <label asp-for="LoanType.LoanName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanType.LoanName" class="form-control" />
                <span asp-validation-for="LoanType.LoanName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanType.Description" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <textarea asp-for="LoanType.Description" class="form-control"></textarea>
                <span asp-validation-for="LoanType.Description" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="Index" class="col-md-5 wal-lead">Display Loans Types</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Save Loan Type" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  9. In the Solution Explorer, under Pages and under LoansTypes, double-click Delete.cshtml
  10. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansTypes.DeleteModel
    
    @{
        ViewData["Title"] = "Delete Loan Type";
    }
    
    <h1 class="bold creme common-font text-center">Delete Loan Type</h1>
    
    <hr color="yellow" />
    
    <h3 class="creme text-center common-font">Are you sure you want to delete this loan type?</h3>
    
    <div class="containment">
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.LoanTypeID)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.LoanTypeID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.LoanName)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.LoanName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.Description)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.Description)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <form method="post">
            <input type="hidden" asp-for="LoanType.LoanTypeID" />
            <a asp-page="./Index" class="wal-lead">Loans Types</a> ::
            <input type="submit" value="Delete Loan Type" class="btn btn-primary" />
        </form>
    </div>
  11. In the Solution Explorer, under Pages and under LoansTypes, double-click Details.cshtml
  12. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansTypes.DetailsModel
    
    @{
        ViewData["Title"] = "Loan Type Details";
    }
    
    <h1 class="bold creme common-font text-center">Loan Type Details</h1>
    
    <hr color="yellow" />
    
    <div class="containment">
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.LoanTypeID)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.LoanTypeID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.LoanName)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.LoanName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            @Html.DisplayNameFor(model => model.LoanType.Description)
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanType.Description)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div>
            <a asp-page="./Index" class="wal-lead">Loans Types</a> ::
            <a asp-page="./Edit" asp-route-id="@Model.LoanType.LoanTypeID" class="wal-lead">Edit this Loan Type</a>
        </div>
    </div>
  13. In the Solution Explorer, under Pages and under LoansTypes, double-click Edit.cshtml
  14. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansTypes.EditModel
    
    @{
        ViewData["Title"] = "Edit Loan Type";
    }
    
    <h1 class="bold creme common-font text-center">Edit/Update Loan Type</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <input type="hidden" asp-for="LoanType.LoanTypeID" />
            <label asp-for="LoanType.LoanName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanType.LoanName" class="form-control" />
                <span asp-validation-for="LoanType.LoanName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanType.Description" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <textarea asp-for="LoanType.Description" class="form-control"></textarea>
                <span asp-validation-for="LoanType.Description" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="./Index" class="col-md-5 wal-lead">Loans Types</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Update Loan Type" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  15. In the Solution Explorer, under Pages and under LoansTypes, double-click Index.cshtml
  16. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansTypes.IndexModel
    
    @{
        ViewData["Title"] = "Loans Types";
    }
    
    <h1 class="bold common-font maint-title text-center">Loans Types</h1>
    
    <table class="table common-font">
        <thead>
            <tr class="tbl-head">
                <th class="text-center">@Html.DisplayNameFor(model => model.LoanType[0].LoanTypeID)</th>
                <th>@Html.DisplayNameFor(model => model.LoanType[0].LoanName)</th>
                <th>@Html.DisplayNameFor(model => model.LoanType[0].Description)</th>
                <th><a asp-page="Create" class="wal-leader">Create Loan Type</a></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.LoanType)
            {
                <tr class="tbl-row">
                    <td class="text-center">@Html.DisplayFor(modelItem => item.LoanTypeID)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanName)</td>
                    <td>@Html.DisplayFor(modelItem => item.Description)</td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@item.LoanTypeID" class="wal-lead">Edit</a> ::
                        <a asp-page="./Details" asp-route-id="@item.LoanTypeID" class="wal-lead">Details</a> ::
                        <a asp-page="./Delete" asp-route-id="@item.LoanTypeID" class="wal-lead">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

Loans Contracts

The company of our application issues various types of loans to many customers. This includes people and businesses. To get a loan, a person or a company must apply for it. For our application, we will create a loan from which an application can be applied for. Then we will create other forms to manage the loans records.

Practical LearningPractical Learning: Managing Loans Contracts

  1. In the Solution Explorer, right-click Pages -> Add -> New Folder
  2. Type LoansContracts as the name of the folder, and press Enter
  3. In the Solution Explorer, under Pages, right-click LoansContracts -> Add -> New Scaffolded Item...
  4. In the middle list of the Add New Scaffolded Item dialog box Explorer, make sure Razor Pages using Entity Framework (CRUD) is selected.
    Click Add
  5. Click the arrow of the Model Class combo box and select LoanContract (WattsALoan1.Models)
  6. Accept the content of the Add Data Context Type dialog box and click Add
  7. In the Solution Explorer, under Pages, expand LoansContracts and expand its Create.cshtml node
  8. Under Create.cshtml, double-click Create.cshtml.cs
  9. Change the document as follows:
    using WattsALoan2.Models;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace WattsALoan2.Pages.LoansContracts
    {
        public class CreateModel : PageModel
        {
            private readonly WattsALoan2.Data.WattsALoan2Context _context;
    
            public CreateModel(WattsALoan2.Data.WattsALoan2Context context)
            {
                _context = context;
            }
    
            public IActionResult OnGet()
            {
                ViewData["EmployeeID"] = new SelectList(_context.Employee, "EmployeeID", "Identification");
                ViewData["LoanTypeID"] = new SelectList(_context.LoanType, "LoanTypeID", "LoanName");
                
                return Page();
            }
    
            [BindProperty]
            public LoanContract LoanContract { get; set; }
    
            // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                _context.LoanContract.Add(LoanContract);
                await _context.SaveChangesAsync();
    
                return RedirectToPage("./Index");
            }
        }
    }
  10. In the Solution Explorer, under Pages and under LoansContracts, double-click Create.cshtml
  11. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansContracts.CreateModel
    
    @{
        ViewData["Title"] = "Create Loan Contract";
    }
    
    <h1 class="bold creme common-font text-center">Process Loan Contract</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.LoanNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.LoanNumber" class="form-control" />
                <span asp-validation-for="LoanContract.LoanNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.DateAllocated" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.DateAllocated" class="form-control" />
                <span asp-validation-for="LoanContract.DateAllocated" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.EmployeeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="LoanContract.EmployeeID" class="form-control" asp-items="ViewBag.EmployeeID"></select>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CustomerFirstName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CustomerFirstName" class="form-control" />
                <span asp-validation-for="LoanContract.CustomerFirstName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CustomerLastName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CustomerLastName" class="form-control" />
                <span asp-validation-for="LoanContract.CustomerLastName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.LoanTypeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="LoanContract.LoanTypeID" class="form-control" asp-items="ViewBag.LoanTypeID"></select>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.LoanAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.LoanAmount" class="form-control" />
                <span asp-validation-for="LoanContract.LoanAmount" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.InterestRate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.InterestRate" class="form-control" />
                <span asp-validation-for="LoanContract.InterestRate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.Periods" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.Periods" class="form-control" />
                <span asp-validation-for="LoanContract.Periods" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.InterestAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.InterestAmount" class="form-control" />
                <span asp-validation-for="LoanContract.InterestAmount" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.FutureValue" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.FutureValue" class="form-control" />
                <span asp-validation-for="LoanContract.FutureValue" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.MonthlyPayment" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.MonthlyPayment" class="form-control" />
                <span asp-validation-for="LoanContract.MonthlyPayment" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.PaymentStartDate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.PaymentStartDate" class="form-control" />
                <span asp-validation-for="LoanContract.PaymentStartDate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CurrentBalance" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CurrentBalance" class="form-control" />
                <span asp-validation-for="LoanContract.CurrentBalance" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="Index" class="wal-lead col-md-5">Loans Contracts</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Save Loan Contract" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  12. In the Solution Explorer, under Pages and under LoansContracts, double-click Delete.cshtml
  13. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansContracts.DeleteModel
    
    @{
        ViewData["Title"] = "Delete Loan Contract";
    }
    
    <h1 class="bold creme common-font text-center">Delete Loan Contract</h1>
    
    <hr color="yellow" />
    
    <h3 class="creme text-center common-font">Are you sure you want to delete this loan contract?</h3>
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanContractID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanContractID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanNumber)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.DateAllocated)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.DateAllocated)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                <label>Processed By</label>
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.Employee.Identification)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CustomerFirstName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CustomerFirstName)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CustomerLastName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CustomerLastName)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanType)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanType.LoanName)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanAmount)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.InterestRate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.InterestRate)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.Periods)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.Periods)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.InterestAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.InterestAmount)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.FutureValue)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.FutureValue)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.MonthlyPayment)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.MonthlyPayment)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.PaymentStartDate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.PaymentStartDate)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CurrentBalance)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CurrentBalance)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <form method="post">
            <input type="hidden" asp-for="LoanContract.LoanContractID" />
            <a asp-page="./Index" class="wal-lead">Loans Contracts</a> ::
            <input type="submit" value="Delete Loan Contract" class="btn btn-primary" />
        </form>
    </div>
  14. In the Solution Explorer, under Pages and under LoansContracts, double-click Details.cshtml
  15. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansContracts.DetailsModel
    
    @{
        ViewData["Title"] = "Loan Contract Details";
    }
    
    <h1 class="bold creme common-font text-center">Loan Contract Details</h1>
    
    <hr color="yellow" />
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanContractID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanContractID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanNumber)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.DateAllocated)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.DateAllocated)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.EmployeeID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.Employee.Identification)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CustomerFirstName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CustomerFirstName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CustomerLastName)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CustomerLastName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanType)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanType.LoanName)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.LoanAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.LoanAmount)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.InterestRate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.InterestRate)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.Periods)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.Periods)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.InterestAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.InterestAmount)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.FutureValue)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.FutureValue)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.MonthlyPayment)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.MonthlyPayment)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.PaymentStartDate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.PaymentStartDate)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.LoanContract.CurrentBalance)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.LoanContract.CurrentBalance)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group">
            <a asp-page="./Index" class="wal-lead">Loans Contracts</a> ::
            <a asp-page="./Edit" asp-route-id="@Model.LoanContract.LoanContractID" class="wal-lead">Edit/update this loan contract</a>
        </div>
    </div>
  16. In the Solution Explorer, under Pages and under LoansContracts, expand Edit.cshtml
  17. In the Solution Explorer, under Pages, under LoansContracts, and under Edit.cshtml, double-click Edit.cshtml.cs
  18. Change the document as follows:
    using System.Linq;
    using WattsALoan2.Models;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace WattsALoan2.Pages.LoansContracts
    {
        public class EditModel : PageModel
        {
            private readonly WattsALoan2.Data.WattsALoan2Context _context;
    
            public EditModel(WattsALoan2.Data.WattsALoan2Context context)
            {
                _context = context;
            }
    
            [BindProperty]
            public LoanContract LoanContract { get; set; }
    
            public async Task<IActionResult> OnGetAsync(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                LoanContract = await _context.LoanContract
                    .Include(l => l.Employee)
                    .Include(l => l.LoanType).FirstOrDefaultAsync(m => m.LoanContractID == id);
    
                if (LoanContract == null)
                {
                    return NotFound();
                }
                
                ViewData["EmployeeID"] = new SelectList(_context.Employee, "EmployeeID", "Identification");
                ViewData["LoanTypeID"] = new SelectList(_context.LoanType, "LoanTypeID", "LoanName");
    
                return Page();
            }
    
            // To protect from overposting attacks, enable the specific properties you want to bind to.
            // For more details, see https://aka.ms/RazorPagesCRUD.
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                _context.Attach(LoanContract).State = EntityState.Modified;
    
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!LoanContractExists(LoanContract.LoanContractID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
    
                return RedirectToPage("./Index");
            }
    
            private bool LoanContractExists(int id)
            {
                return _context.LoanContract.Any(e => e.LoanContractID == id);
            }
        }
    }
  19. In the Solution Explorer, under Pages and under LoansContracts, double-click Edit.cshtml
  20. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansContracts.EditModel
    
    @{
        ViewData["Title"] = "Edit Loan Contract";
    }
    
    <h1 class="bold creme common-font text-center">Edit/Update Loan Contract</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <input type="hidden" asp-for="LoanContract.LoanContractID" />
            <label asp-for="LoanContract.LoanNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.LoanNumber" class="form-control" />
                <span asp-validation-for="LoanContract.LoanNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.DateAllocated" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.DateAllocated" class="form-control" />
                <span asp-validation-for="LoanContract.DateAllocated" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.EmployeeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="LoanContract.EmployeeID" class="form-control" asp-items="ViewBag.EmployeeID"></select>
                <span asp-validation-for="LoanContract.EmployeeID" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CustomerFirstName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CustomerFirstName" class="form-control" />
                <span asp-validation-for="LoanContract.CustomerFirstName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CustomerLastName" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CustomerLastName" class="form-control" />
                <span asp-validation-for="LoanContract.CustomerLastName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.LoanTypeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="LoanContract.LoanTypeID" class="form-control" asp-items="ViewBag.LoanTypeID"></select>
                <span asp-validation-for="LoanContract.LoanTypeID" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.LoanAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.LoanAmount" class="form-control" />
                <span asp-validation-for="LoanContract.LoanAmount" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.InterestRate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.InterestRate" class="form-control" />
                <span asp-validation-for="LoanContract.InterestRate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.Periods" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.Periods" class="form-control" />
                <span asp-validation-for="LoanContract.Periods" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.InterestAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.InterestAmount" class="form-control" />
                <span asp-validation-for="LoanContract.InterestAmount" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.FutureValue" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.FutureValue" class="form-control" />
                <span asp-validation-for="LoanContract.FutureValue" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.MonthlyPayment" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.MonthlyPayment" class="form-control" />
                <span asp-validation-for="LoanContract.MonthlyPayment" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.PaymentStartDate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.PaymentStartDate" class="form-control" />
                <span asp-validation-for="LoanContract.PaymentStartDate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="LoanContract.CurrentBalance" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="LoanContract.CurrentBalance" class="form-control" />
                <span asp-validation-for="LoanContract.CurrentBalance" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="./Index" class="wal-lead col-md-5">Loans Contracts</a>
            <div class="col-md-7 text-center">
                <input type="submit" value="Update Loan Contract" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  21. In the Solution Explorer, under Pages and under LoansContracts, double-click Index.cshtml
  22. Change the document as follows:
    @page
    @model WattsALoan2.Pages.LoansContracts.IndexModel
    
    @{
        ViewData["Title"] = "Loans Contracts";
    }
    
    <h1 class="bold common-font text-center maint-title">Loans Contracts</h1>
    
    <table class="table common-font">
        <thead>
            <tr class="tbl-head">
                <th class="text-center">@Html.DisplayNameFor(model => model.LoanContract[0].LoanContractID)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].LoanNumber)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].DateAllocated)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].EmployeeID)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].CustomerFirstName)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].CustomerLastName)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].LoanType)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].LoanAmount)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].InterestRate)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].Periods)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].InterestAmount)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].FutureValue)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].MonthlyPayment)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].PaymentStartDate)</th>
                <th>@Html.DisplayNameFor(model => model.LoanContract[0].CurrentBalance)</th>
                <th><a asp-page="Create" class="wal-leader">Process Loan Contract</a></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.LoanContract)
            {
                <tr class="tbl-row">
                    <td class="text-center">@Html.DisplayFor(modelItem => item.LoanContractID)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanNumber)</td>
                    <td>@Html.DisplayFor(modelItem => item.DateAllocated)</td>
                    <td>@Html.DisplayFor(modelItem => item.Employee.Identification)</td>
                    <td>@Html.DisplayFor(modelItem => item.CustomerFirstName)</td>
                    <td>@Html.DisplayFor(modelItem => item.CustomerLastName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanType.LoanName)</td>
                    <td>@Html.DisplayFor(modelItem => item.LoanAmount)</td>
                    <td>@Html.DisplayFor(modelItem => item.InterestRate)</td>
                    <td>@Html.DisplayFor(modelItem => item.Periods)</td>
                    <td>@Html.DisplayFor(modelItem => item.InterestAmount)</td>
                    <td>@Html.DisplayFor(modelItem => item.FutureValue)</td>
                    <td>@Html.DisplayFor(modelItem => item.MonthlyPayment)</td>
                    <td>@Html.DisplayFor(modelItem => item.PaymentStartDate)</td>
                    <td>@Html.DisplayFor(modelItem => item.CurrentBalance)</td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@item.LoanContractID" class="wal-lead">Edit</a> ::
                        <a asp-page="./Details" asp-route-id="@item.LoanContractID" class="wal-lead">Details</a> ::
                        <a asp-page="./Delete" asp-route-id="@item.LoanContractID" class="wal-lead">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

Loans Payments

People and businesses that have received a loan must pay it. This means that we must create forms that employees can use to process the necessary payments.

Practical LearningPractical Learning: Managing Loans Contracts

  1. In the Solution Explorer, right-click Pages -> Add -> New Folder
  2. Type Payments as the name of the folder and press Enter
  3. In the Solution Explorer, under Pages, right-click Payments -> Add -> New Scaffolded Item...
  4. In the middle list of the Add New Scaffolded Item dialog box, make sure Razor Pages Using Entity Framework (CRUD) is selected.
    Click Add
  5. Click the arrow of the Model Class combo box and select Payment (WattsALoan1.Models)
  6. Make sure the Data Context Class combo box is displaying WattsALoan1Context (WattsALoan1.Data).
    Click Add
  7. In the Solution Explorer, under Pages, expand Payments and expand Create.cshtml
  8. Under Payments and under Create.cshtml, double-click Create.cshtml.cs
  9. Change the document as follows:
    using WattsALoan2.Models;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace WattsALoan2.Pages.Payments
    {
        public class CreateModel : PageModel
        {
            private readonly WattsALoan2.Data.WattsALoan2Context _context;
    
            public CreateModel(WattsALoan2.Data.WattsALoan2Context context)
            {
                _context = context;
            }
    
            public IActionResult OnGet()
            {
                ViewData["EmployeeID"]     = new SelectList(_context.Employee,     "EmployeeID",     "Identification");
                ViewData["LoanContractID"] = new SelectList(_context.LoanContract, "LoanContractID", "LoanDetails");
    
                return Page();
            }
    
            [BindProperty]
            public Payment Payment { get; set; }
    
            // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                /* When a payment has been made, we need to change the balance of the loan.
                 * To do that, check each loan contract, looking for a loan contract that
                 * uses corresponds to the one whose payment was made her. */
                foreach(LoanContract lc in _context.LoanContract)
                {
                    /* To find the loan contract of the current payment, compare the 
                     * LoanContractID of this payment (the foreign key) and each of the
                     * LoanContractID of the LoanContrats table (the primary key).
                     * If you find that loan contract, ... */
                    if(lc.LoanContractID == Payment.LoanContractID)
                    {
                        // ... subtract the current payment from the balance in the LoanContracts table.
                        lc.CurrentBalance = lc.CurrentBalance - Payment.PaymentAmount;
    
                        // In the SaveChangesAsync() call, the records will be saved.
                    }
                }
    
                _context.Payment.Add(Payment);
                await _context.SaveChangesAsync();
    
    
                return RedirectToPage("./Index");
            }
        }
    }
  10. Under Payments, double-click Create.cshtml
  11. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Payments.CreateModel
    
    @{
        ViewData["Title"] = "Create Payment";
    }
    
    <h1 class="bold creme common-font text-center">Make Loan Payment</h1>
    
    <hr color="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group row common-font">
            <label asp-for="Payment.ReceiptNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.ReceiptNumber" class="form-control" />
                <span asp-validation-for="Payment.ReceiptNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.PaymentDate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.PaymentDate" class="form-control" />
                <span asp-validation-for="Payment.PaymentDate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.EmployeeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="Payment.EmployeeID" class="form-control" asp-items="ViewBag.EmployeeID"></select>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.LoanContractID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="Payment.LoanContractID" class="form-control" asp-items="ViewBag.LoanContractID"></select>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.PaymentAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.PaymentAmount" class="form-control" />
                <span asp-validation-for="Payment.PaymentAmount" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="form-group row">
            <a asp-page="Index" class="col-md-5 wal-lead">Loans Payments</a>
            <div class="text-center col-md-7">
                <input type="submit" value="Save Loan Payment" class="btn btn-primary" />
            </div>
        </div>
    </form>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  12. In the Solution Explorer, under Pages and under Payments, double-click Delete.cshtml
  13. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Payments.DeleteModel
    
    @{
        ViewData["Title"] = "Delete Payment";
    }
    
    <h1 class="bold creme common-font text-center">Delete Loan Payment</h1>
    
    <hr color="yellow" />
    
    <h3 class="creme text-center common-font">Are you sure you want to delete this payment?</h3>
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentID)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.ReceiptNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.ReceiptNumber)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentDate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentDate)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.EmployeeID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.Employee.Identification)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.LoanContractID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.LoanContract.LoanDetails)
            </div>
        </div>
    
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentAmount)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <form method="post">
            <input type="hidden" asp-for="Payment.PaymentID" />
            <a asp-page="./Index" class="text-center wal-lead">Loans Payments</a> ::
            <input type="submit" value="Delete Payment" class="btn btn-warning" />
        </form>
    </div>
  14. In the Solution Explorer, under Pages and under Payments, double-click Details.cshtml
  15. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Payments.DetailsModel
    
    @{
        ViewData["Title"] = "Payment Details";
    }
    
    <h1 class="bold creme common-font text-center">Payment Details</h1>
    
    <hr color="yellow" />
    
    <div class="containment">
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentID)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.ReceiptNumber)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.ReceiptNumber)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentDate)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentDate)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.EmployeeID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.Employee.Identification)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.LoanContractID)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.LoanContract.LoanDetails)
            </div>
        </div>
        <div class="form-group row common-font creme">
            <div class="control-label col-md-5">
                @Html.DisplayNameFor(model => model.Payment.PaymentAmount)
            </div>
            <div class="col-md-7">
                @Html.DisplayFor(model => model.Payment.PaymentAmount)
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div>
            <a asp-page="./Index" class="text-center wal-lead">Loans Payments</a> ::
            <a asp-page="./Edit" asp-route-id="@Model.Payment.PaymentID" class="wal-lead">Edit this Payment</a>
        </div>
    </div>
  16. In the Solution Explorer, under Pages and under Payments, expand Edit.cshtml
  17. Under Payments and under Edit.cshtml, double-click Edit.cshtml.cs
  18. Change the document as follows:
    using System.Linq;
    using WattsALoan2.Models;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    
    namespace WattsALoan2.Pages.Payments
    {
        public class EditModel : PageModel
        {
            private readonly WattsALoan2.Data.WattsALoan2Context _context;
    
            public EditModel(WattsALoan2.Data.WattsALoan2Context context)
            {
                _context = context;
            }
    
            [BindProperty]
            public Payment Payment { get; set; }
    
            public async Task<IActionResult> OnGetAsync(int? id)
            {
                if (id == null)
                {
                    return NotFound();
                }
    
                Payment = await _context.Payment
                    .Include(p => p.Employee)
                    .Include(p => p.LoanContract).FirstOrDefaultAsync(m => m.PaymentID == id);
    
                if (Payment == null)
                {
                    return NotFound();
                }
                
                ViewData["EmployeeID"]     = new SelectList(_context.Employee,     "EmployeeID",     "Identification");
                ViewData["LoanContractID"] = new SelectList(_context.LoanContract, "LoanContractID", "LoanDetails");
                
                return Page();
            }
    
            // To protect from overposting attacks, enable the specific properties you want to bind to.
            // For more details, see https://aka.ms/RazorPagesCRUD.
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                _context.Attach(Payment).State = EntityState.Modified;
    
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PaymentExists(Payment.PaymentID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
    
                return RedirectToPage("./Index");
            }
    
            private bool PaymentExists(int id)
            {
                return _context.Payment.Any(e => e.PaymentID == id);
            }
        }
    }
  19. Under Payments, double-click Edit.cshtml
  20. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Payments.EditModel
    
    @{
        ViewData["Title"] = "Edit Payment";
    }
    
    <h1 class="bold creme common-font text-center">Edit/Update Loan Payment</h1>
    
    <hr class="yellow" />
    
    <form method="post" class="containment">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="row form-group common-font">
            <input type="hidden" asp-for="Payment.PaymentID" />
            <label asp-for="Payment.ReceiptNumber" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.ReceiptNumber" class="form-control" />
                <span asp-validation-for="Payment.ReceiptNumber" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.PaymentDate" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.PaymentDate" class="form-control" />
                <span asp-validation-for="Payment.PaymentDate" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group common-font row">
            <label asp-for="Payment.EmployeeID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="Payment.EmployeeID" class="form-control" asp-items="ViewBag.EmployeeID"></select>
                <span asp-validation-for="Payment.EmployeeID" class="text-danger"></span>
            </div>
        </div>
        <div class="row form-group common-font">
            <label asp-for="Payment.LoanContractID" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <select asp-for="Payment.LoanContractID" class="form-control" asp-items="ViewBag.LoanContractID"></select>
                <span asp-validation-for="Payment.LoanContractID" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group row common-font">
            <label asp-for="Payment.PaymentAmount" class="control-label col-md-5 creme"></label>
            <div class="col-md-7">
                <input asp-for="Payment.PaymentAmount" class="form-control" />
                <span asp-validation-for="Payment.PaymentAmount" class="text-danger"></span>
            </div>
        </div>
    
        <hr color="yellow" />
    
        <div class="row form-group">
            <div class="col-md-5">
                <a asp-page="./Index" class="wal-lead">Loans Payments</a>
            </div>
            <div class="col-md-5 text-center">
                <input type="submit" value="Update Loan Payment" class="btn btn-warning" />
            </div>
        </div>
    </form>
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
  21. In the Solution Explorer, under Pages and under Payments, double-click Index.cshtml
  22. Change the document as follows:
    @page
    @model WattsALoan2.Pages.Payments.IndexModel
    
    @{
        ViewData["Title"] = "Loans Payments";
    }
    
    <h1 class="maint-title bold common-font text-center">Loans Payments</h1>
    
    <table class="table common-font">
        <thead>
            <tr class="tbl-head">
                <th class="text-center">@Html.DisplayNameFor(model => model.Payment[0].PaymentID)</th>
                <th>@Html.DisplayNameFor(model => model.Payment[0].ReceiptNumber)</th>
                <th>@Html.DisplayNameFor(model => model.Payment[0].PaymentDate)</th>
                <th>@Html.DisplayNameFor(model => model.Payment[0].Employee.EmployeeID)</th>
                <th>@Html.DisplayNameFor(model => model.Payment[0].LoanContract.LoanNumber)</th>
                <th>@Html.DisplayNameFor(model => model.Payment[0].PaymentAmount)</th>
                <th><a asp-page="Create" class="wal-leader">Process Loan Payment</a></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Payment)
            {
            <tr class="tbl-row">
                <td class="text-center">@Html.DisplayFor(modelItem => item.PaymentID)</td>
                <td>@Html.DisplayFor(modelItem => item.ReceiptNumber)</td>
                <td>@Html.DisplayFor(modelItem => item.PaymentDate)</td>
                <td>@Html.DisplayFor(modelItem => item.Employee.Identification)</td>
                <td>@Html.DisplayFor(modelItem => item.LoanContract.LoanDetails)</td>
                <td>@Html.DisplayFor(modelItem => item.PaymentAmount)</td>
                <td>
                    <a asp-page="./Edit"    asp-route-id="@item.PaymentID" class="wal-lead">Edit</a> ::
                    <a asp-page="./Details" asp-route-id="@item.PaymentID" class="wal-lead">Details</a> ::
                    <a asp-page="./Delete"  asp-route-id="@item.PaymentID" class="wal-lead">Delete</a>
                </td>
            </tr>
            }
        </tbody>
    </table>

Creating a Database

To use the Code First From Database option of the entity framework, in your project, add an ADO.NET Entity Data Model to your Models folder. In the wizard, select the Code First From Database option and follow the wizard.

Practical LearningPractical Learning: Creating a Database

  1. On the main menu of Microsoft Visual Studio, click Tools -> NuGet Package Manager -> Mackage Manager Console
  2. In the prompt of the Package Manager Console window, type:
    Add-Migration InitialCreate
  3. Press Enter
  4. Again, in the prompt of the Package Manager Console window, type:
    Update-Database
  5. Press Enter

Finalizing the Application

A regular web-based application should have a home page. From that page, most objects of the application can be accessed. In an ASP.NET application, both MVC or .NET Core, you usually use a _Layout,cshtml and an Index.cshtml documents to create a home page.

Practical LearningPractical Learning: Finalizing the Application

  1. In the Solution Explorer, under Pages, expand Shared
  2. Double-click _Layout.cshtml
  3. Change the document as follows:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - Watts' A Loan</title>
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" />
        <link rel="stylesheet" href="~/css/WattsALoan.css" />
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container top-menu-holder">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">Watts' A Loan</a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Employees/Index">Employees</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/LoansContracts/Index">Loans Contracts</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Payments/Index">Payments</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/LoansTypes/Index">Loans Types</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Index">About Us</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
        <div class="container">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
        </div>
    
        <footer class="border-top footer text-muted">
            <div class="container text-center">
                <span class="creme common-font">&copy; @DateTime.Today.Year - Watts' A Loan -</span> <a asp-area="" asp-page="/Privacy" class="wal-lead">Privacy</a>
    </div>
        </footer>
    
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    
        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>
  4. In the Solution Explorer, below Shared, double-click Index.cshtml
  5. Change the class as follows:
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Watts&#039; A Loan";
    }
    
    <div class="jumbotron">
        <h1 class="text-center common-font">Watts&#039; A Loan</h1>
        <p class="text-center">
            <img src="~/images/wal1.png" alt="Watts A Loan" width="650" height="200" />
        </p>
    </div>
    
    <hr color="yellow" />
    
    <div class="row">
        <div class="col-md-9">
            <div class="row common-font">
                <div class="col-md-2">
                    <img src="~/images/wal2.png" alt="Watts A Loan - Personal Loans" width="100" height="71" class="bordered" />
                </div>
                <div class="col-md-10">
                    <h4 class="creame">Personal Loans</h4>
                    <p class="texting">Our most popular program is for personal loans, usually as cash, delivered directly from our office. The application process is very fast and easy. To start, access our loan application.</p>
                </div>
            </div>
    
            <hr color="white" />
            <div class="row">
                <div class="col-md-6">
                    <div class="row common-font">
                        <div class="col-md-4">
                            <img src="~/images/wal3.png" alt="Watts A Loan - Furniture Loans" width="100" height="75" class="bordered" />
                        </div>
                        <div class="col-md-8">
                            <h4>Furniture Loans</h4>
                            <p class="texting">Another one of our popular loan programs is to assist customers in buying furniture.</p>
                        </div>
                    </div>
                </div>
    
                <div class="col-md-6">
                    <div class="row common-font">
                        <div class="col-md-4">
                            <img src="~/images/wal4.png" alt="Watts A Loan - Car Financing" width="100" height="75" class="bordered" />
                        </div>
                        <div class="col-md-8">
                            <h4>Car Financing</h4>
                            <p class="texting">Another one of our popular loan program is to assist customers in buying furniture.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="menu-box">
                <ul>
                    <li>Loan Application</li>
                    <li>Loan Process</li>
                    <li>Types of Loans</li>
                    <li>Financial Aid</li>
                    <li>Students Concerns</li>
                    <li>Car Financing</li>
                    <li>Musical Instruments</li>
                    <li>Small Business Loan</li>
                    <li>Documentation</li>
                    <li>Newsletters</li>
                </ul>
            </div>
        </div>
    </div>

Testing the Application

After creating the necessary documents, you can execute the application to access and test its objects.

Practical LearningPractical Learning: Creating a Database

  1. To execute, on the main menu, click Debug -> Start Without Debugging

    Watts's A Loan - Home Page

  2. In the home page, click Employees

    >Watts's A Loan - Employees

  3. Click Hire Employee

    Watts's A Loan - Employees

  4. Enter the values from the following table for each record and click Save Employee Record after each record:

    Employee # First Name Last Name Employee Title
    293-747 Catherine Watts Owner - General Manager
    836-486 Thomas Felton Accounts Representative
    492-947 Caroline Wernick Assistant Manager
    240-750 Catherine Donato Accounts Representative
    804-685 Melissa Browns Customer Accounts Representative
    429-374 Denise Leighton Accounts Manager

    Watts' A Loan - Hire Employee

    >Watts's A Loan - Employees

    INSERT Employees
    VALUES(N'293-747', N'Catherine', N'Watts',    N'Owner'),
          (N'836-486', N'Thomas',    N'Felton',   N'Accounts Representative'),
          (N'492-947', N'Caroline',  N'Wernick',  N'Assistant Manager'),
          (N'240-750', N'Catherine', N'Donato',   N'Accounts Representative'),
          (N'804-685', N'Melissa',   N'Browns',   N'Customer Accounts Representative'),
          (N'429-374', N'Denise',    N'Leighton', N'Accounts Manager');
    GO
  5. In the top bar, click Loans Types

    Watts' A Loan - Loans Types

  6. Click Create Loan Type

    Watts' A Loan - Create Loan Type

  7. Enter the values from the following table for each record. Click the Save Loan Type button after each record:

    Loan Type Description
    Personal Cash This type of loan is provided as cash to a customer.
    Car Financing We partner with various car dealers in our metropolitan area. Customers typically apply for car loans from dealerships. If/When a loan has been approved, we issue the necessary amount.
    Boat Financing Like cars, boats are financed through boat dealers, repair shops, or manufacturers. A customer can apply for a loan from one of those businesses or directly from our office.
    Furniture Purchase We provide low interest loans for people who need to purchase some furniture but may not have all the necessary cash. Customers may apply for a loan to finance furniture from a retail store.
    Musical Instrument Among the types of loans that fit our interests are those from a regular musical instrument such as a piano to a complete recording studio.

    Watts's A Loan - Loans Contracts

    INSERT INTO LoanTypes(LoanName, [Description])
    VALUES(N'Personal Cash', N'This type of loan is provided as cash to a customer.'),
          (N'Car Financing', N'We partner with various car dealers in our metropolitan area. Customers typically apply for car loans from dealerships. If/When a loan has been approved, we issue the necessary amount.'),
          (N'Boat Financing', N'Like cars, boats are financed through boat dealers, repair shops, or manufacturers. A customer can apply for a loan from one of those businesses or directly from our office.'),
          (N'Furniture Purchase', N'We provide low interest loans for people who need to purchase some furniture but may not have all the necessary cash. Customers may apply for a loan to finance furniture from a retail store.'),
          (N'Musical Instrument', N'Among the types of loans that fit our interests are those from a regular musical instrument such as a piano to a complete recording studio.');
    GO
  8. Click Loans Contracts:

    Watts's A Loan - Loans Contracts

  9. Click Process Loan Contract:

    Watts's A Loan - Loans Contract

  10. Enter the values from the following table; click Save Loan Contract for each record:
    Loan Number:        100001
    Date Allocated:     January 18, 2021
    Employee #:         429-374
    First Name:         Joanne
    Last Name:          Kennan
    Loan Type:          Personal Cash
    Loan Amount:        2500
    Interest Rate:      14.65
    Periods:            36
    Interest Amount:    1,483.51
    Future Value:       3,983.51
    Monthly Payment:    87.44
    Current Balance:    3,983.51
    Payment Start Date: March 1st, 2021

    Watts' A Loan - Loan Application

  11. Create other records with the following values and click Save Loan Contract after each record:
    Loan Number:        100002
    Date Allocated:     February 22, 2021
    Employee #:         492-947
    First Name:         Stephen
    Last Name:          Haller
    Loan Type:          Boat Financing
    Loan Amount:        46500
    Interest Rate:      4.95
    Periods:            60
    Interest Amount:    13,027.79 
    Future Value:       59,527.79
    Monthly Payment:    876.45
    Payment Start Date: March 1st, 2021
    Current Balance:    59,527.79
  12. Create another record with the following values:
    Loan Number:        100003
    Date Allocated:     March 12, 2021
    Employee #:         429-374
    First Name:         Annette
    Last Name:          Vargas
    Loan Type:          Furniture Purchase
    Loan Amount:        2258.75
    Interest Rate:      17.35
    Periods:            36
    Interest Amount:    1,528.29
    Future Value:       3,787.04
    Monthly Payment:    80.92
    Payment Start Date: May 1st, 2021
    Current Balance:    3,787.04
  13. Create another record with the following values:
    Loan Number:        100004
    Date Allocated:     March 12, 2021
    Employee #:         836-486
    First Name:         Gérard
    Last Name:          Haloney
    Loan Type:          Car Financing
    Loan Amount:        22748
    Interest Rate:      10.25
    Periods:            60
    Interest Amount:    15,146.30
    Future Value:       37,894.30
    Monthly Payment:    486.13
    Payment Start Date: May 1st, 2021
    Current Balance:    37,894.30

    Watts' A Loan - Loans Contracts

  14. Click Payments

    Watts' A Loan - Payments

  15. Click Make Loan Payment

    Watts' A Loan - Process Loan Payment

  16. Enter the following values:
    Receipt Number: 385970
    Payment Date:   March 03, 2021
    Processed By:   429-374
    Loan Contract:  100001
    Payment Amount: 87.44

    Watts' A Loan - Loan Application

    Watts' A Loan - Payments

  17. Click Save Loan Payment
  18. Again, click Process Loan Payment and enter the following values:
    Receipt Number: 953746
    Payment Date:   March 30, 2021
    Processed By:   492-947
    Loan Contract:  100002
    Payment Amount: 876.45
  19. Click Save Loan Payment
  20. Close your programming environment

Home Copyright © 2017-2025, FunctionX Home