Introduction

Razor Pages is one of the techniques you can use to create an ASP.NET Core application. In this exercise, we will create a small application, add a webform to it, equip it with text boxes, and perform simple calculations.

ApplicationPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select C#
  4. In the list of projects templates, click ASP.NET Core Web App (Razor Pages)
  5. Click Next
  6. Specify the Project Name as PayrollEvaluation2
  7. Click Next
  8. In Framework combo box of the Additional Information dialog box, select the highest version (.NET 9.0 (Standart Term Support).
    Click Create
  9. In the Solution Explorer, expand wwwroot and expand css
  10. Double-click site.css
  11. In the document, add the following styles:
    html {
      font-size: 14px;
    }
    
    @media (min-width: 768px) {
      html {
        font-size: 16px;
      }
    }
    
    .btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
      box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
    }
    
    html {
      position: relative;
      min-height: 100%;
    }
    
    body {
      margin-bottom: 60px;
    }
    
    .form-floating > .form-control-plaintext::placeholder, .form-floating > .form-control::placeholder {
      color: var(--bs-secondary-color);
      text-align: end;
    }
    
    .form-floating > .form-control-plaintext:focus::placeholder, .form-floating > .form-control:focus::placeholder {
      text-align: start;
    }
    
    .form-control { border: 1px solid Silver; }
    .encloser    { margin:      auto;
                   width:       450px; }
    .common-font { font-family: Garamond, Georgia, Cambria, 'Times New Roman', Times, serif }
  12. In the Solution Explorer, expand Pages
  13. Below Pages, double-click Index.cshtml
  14. Change the document as follows:
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Payroll Evaluation";
    
        string? strEmployeeName     = string.Empty;
        string? strFirstName        = string.Empty;
        string? strLastName         = string.Empty;
    
        string? strHourlySalary     = string.Empty;
    
        string? strWeek1Monday      = string.Empty;
        string? strWeek1Tuesday     = string.Empty;
        string? strWeek1Wednesday   = string.Empty;
        string? strWeek1Thursday    = string.Empty;
        string? strWeek1Friday      = string.Empty;
    
        string? strWeek2Monday      = string.Empty;
        string? strWeek2Tuesday     = string.Empty;
        string? strWeek2Wednesday   = string.Empty;
        string? strWeek2Thursday    = string.Empty;
        string? strWeek2Friday      = string.Empty;
    
        string? strWeek1RegularTime = string.Empty;
        string? strWeek1OverTime    = string.Empty;
        string? strWeek1RegularPay  = string.Empty;
        string? strWeek1OvertimePay = string.Empty;
    
        string? strWeek2RegularTime = string.Empty;
        string? strWeek2OverTime    = string.Empty;
        string? strWeek2RegularPay  = string.Empty;
        string? strWeek2OvertimePay = string.Empty;
    
        string? strNetPay           = string.Empty;
    
        if (Request.HasFormContentType)
        {
            strFirstName            = Request.Form["txtFirstName"];
            strLastName             = Request.Form["txtLastName"];
    
            double hourlySalary     = double.Parse(Request.Form["txtHourlySalary"]!);
             
            double wk1Monday        = double.Parse(Request.Form["txtWeek1Monday"]!);
            double wk1Tuesday       = double.Parse(Request.Form["txtWeek1Tuesday"]!);
            double wk1Wednesday     = double.Parse(Request.Form["txtWeek1Wednesday"]!);
            double wk1Thursday      = double.Parse(Request.Form["txtWeek1Thursday"]!);
            double wk1Friday        = double.Parse(Request.Form["txtWeek1Friday"]!);
    
            double wk2Monday        = double.Parse(Request.Form["txtWeek2Monday"]!);
            double wk2Tuesday       = double.Parse(Request.Form["txtWeek2Tuesday"]!);
            double wk2Wednesday     = double.Parse(Request.Form["txtWeek2Wednesday"]!);
            double wk2Thursday      = double.Parse(Request.Form["txtWeek2Thursday"]!);
            double wk2Friday        = double.Parse(Request.Form["txtWeek2Friday"]!);
    
            double wk1TimeWorked    = wk1Monday + wk1Tuesday + wk1Wednesday + wk1Thursday + wk1Friday;
            double wk2TimeWorked    = wk2Monday + wk2Tuesday + wk2Wednesday + wk2Thursday + wk2Friday;
    
            double wk1RegularTime   = 0.00;
            double wk1OverTime      = 0.00;
            double wk1RegularPay    = 0.00;
            double wk1OvertimePay   = 0.00;
    
            double wk2RegularTime   = 0.00;
            double wk2OverTime      = 0.00;
            double wk2RegularPay    = 0.00;
            double wk2OvertimePay   = 0.00;
    
            // If the employee worked below 40 hours, there is no overtime
            if (wk1TimeWorked < 40)
            {
                wk1RegularTime      = wk1TimeWorked;
                wk1OverTime         = 0.00;
                wk1RegularPay       = hourlySalary * wk1TimeWorked;
                wk1OvertimePay      = 0.00;
            } // If the employee worked over 40 hours, calculate the overtime
            else // if (wk1TimeWorked >= 40)
            {
                wk1RegularTime      = 40;
                wk1OverTime         = wk1TimeWorked - 40;
                wk1RegularPay       = hourlySalary * 40;
                wk1OvertimePay      = wk1OverTime * hourlySalary * 1.50;
            }
    
            if (wk2TimeWorked < 40)
            {
                wk2RegularTime      = wk2TimeWorked;
                wk2OverTime         = 0.00;
                wk2RegularPay       = hourlySalary * wk2TimeWorked;
                wk2OvertimePay      = 0.00;
            }
            else // if (wk2TimeWorked >= 40)
            {
                wk2RegularTime      = 40;
                wk2OverTime         = wk2TimeWorked - 40;
                wk2RegularPay       = hourlySalary * 40;
                wk2OvertimePay      = hourlySalary * 1.50 * wk2OverTime;
            }
    
            var netPay              = wk1RegularPay + wk1OvertimePay;
    
            strEmployeeName         = strFirstName + " " + strLastName;
            strHourlySalary         = string.Format("{0:F}", hourlySalary);
    
            strWeek1Monday          = string.Format("{0:F}", wk1Monday);
            strWeek1Tuesday         = string.Format("{0:F}", wk1Tuesday);
            strWeek1Wednesday       = string.Format("{0:F}", wk1Wednesday);
            strWeek1Thursday        = string.Format("{0:F}", wk1Thursday);
            strWeek1Friday          = string.Format("{0:F}", wk1Friday);
    
            strWeek2Monday          = string.Format("{0:F}", wk2Monday);
            strWeek2Tuesday         = string.Format("{0:F}", wk2Tuesday);
            strWeek2Wednesday       = string.Format("{0:F}", wk2Wednesday);
            strWeek2Thursday        = string.Format("{0:F}", wk2Thursday);
            strWeek2Friday          = string.Format("{0:F}", wk2Friday);
    
            strWeek1RegularTime     = string.Format("{0:F}", wk1RegularTime);
            strWeek1OverTime        = string.Format("{0:F}", wk1OverTime);
            strWeek1RegularPay      = string.Format("{0:F}", wk1RegularPay);
            strWeek1OvertimePay     = string.Format("{0:F}", wk1OvertimePay);
    
            strWeek2RegularTime     = string.Format("{0:F}", wk2RegularTime);
            strWeek2OverTime        = string.Format("{0:F}", wk2OverTime);
            strWeek2RegularPay      = string.Format("{0:F}", wk2RegularPay);
            strWeek2OvertimePay     = string.Format("{0:F}", wk2OvertimePay);
    
            strNetPay               = string.Format("{0:F}", netPay);
        }
    }
    
    <h1 class="display-5 text-center fw-bold common-font">Payroll Evaluation</h1>
    
    <hr />
    
    <form method="post" class="common-font encloser">
        <div class="row mb-2">
            <label for="txtFirstName" class="col-form-label col-sm-4 fw-bold">First Name:</label>
            <div class="col-sm-4">
                <input id="txtFirstName" name="txtFirstName" class="form-control" value="@strFirstName" />
            </div>
        </div>
    
        <div class="row mb-2">
            <label for="txtLastName" class="fw-bold col-sm-4 col-form-label">Last Name:</label>
            <div class="col-sm-4">
                <input id="txtLastName" name="txtLastName" class="form-control" value="@strLastName" />
            </div>
        </div>
    
        <div class="row mb-2">
            <label for="txtHourlySalary" class="fw-bold col-sm-4 col-form-label">Hourly Salary:</label>
            <div class="col-sm-4">
                <input id="txtHourlySalary" name="txtHourlySalary"
                       class="form-control" value="@strHourlySalary" />
            </div>
        </div>
    
        <div class="fw-bold row mb-2">
            <div class="col-sm-5">
                <label class="col-form-label">Time Worked</label>
            </div>
            <div class="col-sm-4">
                <label>Week 1</label>
            </div>
            <div class="col-sm-3">
                <label>Week 2</label>
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1Monday" class="fw-bold col-sm-5 col-form-label">Monday:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1Monday" name="txtWeek1Monday"
                       class="form-control" value="@strWeek1Monday" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2Monday" name="txtWeek2Monday"
                       class="form-control" value="@strWeek2Monday" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1Tuesday" class="fw-bold col-sm-5 col-form-label">Tuesday:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1Tuesday" name="txtWeek1Tuesday"
                       class="form-control" value="@strWeek1Tuesday" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2Tuesday" name="txtWeek2Tuesday"
                       class="form-control" value="@strWeek2Tuesday" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1Wednesday" class="fw-bold col-sm-5 col-form-label">Wednesday:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1Wednesday" name="txtWeek1Wednesday"
                       class="form-control" value="@strWeek1Wednesday" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2Wednesday" name="txtWeek2Wednesday"
                       class="form-control" value="@strWeek2Wednesday" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1Thursday" class="fw-bold col-sm-5 col-form-label">Thursday:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1Thursday" name="txtWeek1Thursday"
                       class="form-control" value="@strWeek1Thursday" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2Thursday" name="txtWeek2Thursday"
                       class="form-control" value="@strWeek2Thursday" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1Friday" class="fw-bold col-sm-5 col-form-label">Friday:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1Friday" name="txtWeek1Friday"
                       class="form-control" value="@strWeek1Friday" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2Friday" name="txtWeek2Friday"
                       class="form-control" value="@strWeek2Friday" />
            </div>
        </div>
    
        <div class="row mb-2">
            <label class="fw-bold col-sm-6 col-form-label"></label>
            <div class="col-sm-6">
                <input type="submit" value="Calculate" id="btnCalculate" class="btn btn-primary" />
            </div>
        </div>
    
        <hr />
    
        <div class="row mb-2">
            <label for="txtEmployeeName" class="fw-bold col-sm-4 col-form-label">Employee Name:</label>
            <div class="col-sm-8">
                <input id="txtEmployeeName" name="txtEmployeeName"
                       class="form-control" value="@strEmployeeName" />
            </div>
        </div>
    
        <div class="fw-bold row mb-2">
            <div class="col-sm-5">
                <label class="col-form-label">Pay Summary</label>
            </div>
            <div class="col-sm-4">
                <label>Week 1</label>
            </div>
            <div class="col-sm-3">
                <label>Week 2</label>
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1RegularTime" class="fw-bold col-form-label">Regular Time:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1RegularTime" class="form-control" value="@strWeek1RegularTime" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2RegularTime" name="txtWeek2RegularTime"
                       class="form-control" value="@strWeek2RegularTime" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1OverTime" class="fw-bold col-form-label">Over Time:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1OverTime" class="form-control" value="@strWeek1OverTime" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2OverTime" name="txtWeek2OverTime"
                       class="form-control" value="@strWeek2OverTime" />
            </div>
        </div>
        
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1RegularPay" class="fw-bold col-form-label">Regular Pay:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1RegularPay" class="form-control" value="@strWeek1RegularPay" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2RegularPay" name="txtWeek2RegularPay"
                       class="form-control" value="@strWeek2RegularPay" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label for="txtWeek1OvertimePay" class="fw-bold col-form-label">Overtime Pay:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtWeek1OvertimePay" class="form-control" value="@strWeek1OvertimePay" />
            </div>
            <div class="col-sm-4">
                <input id="txtWeek2OvertimePay" name="txtWeek2OvertimePay"
                       class="form-control" value="@strWeek2OvertimePay" />
            </div>
        </div>
    
        <div class="row mb-2">
            <div class="col-sm-4">
                <label></label>
            </div>
            <div class="col-sm-4 text-center">
                <label for="txtNetPay" class="fw-bold col-form-label">Net Pay:</label>
            </div>
            <div class="col-sm-4">
                <input id="txtNetPay" class="form-control" value="@strNetPay" />
            </div>
        </div>
    </form>
  15. In the Solution Explorer, below Pages, expand Shared
  16. Double-click _Layout.cshtm
  17. 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"] - Payroll Evaluation</title>
        <script type="importmap"></script>
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/PayrollEvaluation1.styles.css" asp-append-version="true" />
    </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">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">Payroll Evaluation</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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="/Index">Home</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">
                <p class="common-font text-center">&copy; 2001-@DateTime.Today.Year - Payroll Evaluation - <a asp-area="" asp-page="/Privacy">Privacy</a></p>
            </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>
  18. To execute the application, press Ctrl + F5:

    Payroll Evaluation

  19. In the text boxes, enter the following values:
    First Name:    Michael
    Last Name:     Carlock
    Hourly Salary: 28.46
                Week 1      Week 2
    Monday:        7        6.5
    Tuesday:       8        7.5
    Wednesday:     6.5      8
    Thursday:      8.5      6.5
    Friday:        7.5      7

    Payroll Evaluation

  20. Click the Calculate button:

    Payroll Evaluation

  21. Change the values in the top text boxes as follows:
    First Name:    Catherine
    Last Name:     Busbey
    Hourly Salary: 24.37
                Week 1      Week 2
    Monday:        9.5      8
    Tuesday:       8        7.5
    Wednesday:     10.5     8
    Thursday:      9        6.5
    Friday:        8.5      7.5
  22. Click the Calculate button:

    Payroll Evaluation

  23. Close the browser and return to your programming environment
  24. Close your programming environment

Home Copyright © 2021-2025, FunctionX Thursday 19 December 2024, 13:27 Home