Introduction

The Apartments Rental Management is a fictitious company that owns an apartment building and rents various types of apartments to its tenants. In this exercise, we will use ASP.NET MVC to create a web-based application to manage rent for the company.

Practical LearningPractical Learning: Introducing the Application

  1. Start Microsoft Visual Studio (this application was created in Microsoft Visual Studio 2019)
  2. In the Visual Studio 2019 dialog box, click Create a New Project
  3. In the languages combo box, select C# and, in the right list of projects templates, click ASP.NET Web Application (.NET Framework)

    Create a New Project

  4. Click Next
  5. In the Configure Your New Project dialog box, change the Project Name to ApartmentsRentalManagement1
  6. Accept or change the project Location.
    In the Framework combo box, select the highest version of the .NET library (.NET Framework 4.8)

    Crate a New ASP.NET Application

    Click Create
  7. In the Create a New ASP.NET Web Application dialog box, click the Web API icon and uncheck the Configure For HTTPS check box

    Crate a New ASP.NET Application

  8. Click Create
  9. In the Solution Explorer, right-click ApartmentsRentalManagement -> Add -> New Folder
  10. Type Images
  11. Copy the follwing photo to the Images folder:

    Apartments Rental Management

  12. In the Solution Explorer, right-click Content -> Add -> Style Sheet
  13. Type RentManagement
  14. Press Enter
  15. Add the following formats:
    body {
        background-color: #FFF;
    }
    
    .bold                { font-weight:      600;        }
    .blue                { color:            #286090;    }
    .top-padding         { padding-top:      0.50em;     }
    .top-banner          { top:              0;
                          left:              0;
                          right:             0;
                          z-index:           1050;
                          height:            20em;
                          position:          fixed;
                          background-image:  url(/Images/arm1.png); }
    .containment        { margin:            auto;
                          width:             400px;      }
    .jumbotron          { background-color:  white;      }
    .form-control       { color:             #286090;    }
    .form-control:focus { color:             dodgerblue; }
    .navbar-top-fixed   { left:              0;
                          right:             0;
                          top:               20em;
                          z-index:           1100;
                          position:          fixed;
                          border-width:      0 0 1px; }
    .common-font        { font-family:       Georgia, Garamond, 'Times New Roman', serif; }
    .navbar-top-fixed .navbar-collapse { max-height: 340px; }
    
    @media (min-width: 768px) {
        .navbar-top-fixed .navbar-collapse {
            padding-right: 0;
            padding-left: 0;
        }
    }
    
    @media (max-device-width: 480px) and (orientation: landscape) {
        .navbar-top-fixed .navbar-collapse {
            max-height: 200px;
        }
    }
    
    @media (min-width: 768px) {
        .navbar-top-fixed {
            border-radius: 0;
        }
    }
  16. In the Solution Explorer, expand App_Start and double-click BundleConfig.cs
  17. Change the document as follows:
    using System.Web.Optimization;
    
    namespace ApartmentsRentalManagement1
    {
        public class BundleConfig
        {
            // For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
            public static void RegisterBundles(BundleCollection bundles)
            {
                bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                            "~/Scripts/jquery-{version}.js"));
    
                bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                            "~/Scripts/jquery.validate*"));
    
                // Use the development version of Modernizr to develop with and learn from. Then, when you're
                // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
                bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                            "~/Scripts/modernizr-*"));
    
                bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                          "~/Scripts/bootstrap.js"));
    
                bundles.Add(new StyleBundle("~/Content/css").Include(
                          "~/Content/bootstrap.css",
                          "~/Content/site.css",
                          "~/Content/RentManagement.css"));
            }
        }
    }
  18. In the Solution Explorer, expand Views and expand Shared
  19. In the Solution Explorer, under Shared, double-click _Layout.cshtml to open it
  20. Change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Apartments Rental Management :: @ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <div class="top-banner"></div>
        
        <div class="navbar navbar-inverse navbar-top-fixed">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("HOME", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("RENT MANAGEMENT", "CentralPoint", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("LEASE APPLICATION", "Index", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("COMMUNITY", "Index", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("FLOOR PLANS", "Index", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("CAREERS", "Index", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("ABOUT ARM", "About", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("CONTACT US", "Contact", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)</li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p class="text-center common-font blue">&copy; @DateTime.Now.Year - Apartments Rental Management</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  21. Close the _Layout.cshtml tab
  22. When asked whether you want to save, click Yes
  23. In the Solution Explorer, under Views and under Shared, right-click _Layout.cshtml and click Copy
  24. Still in the Solution Explorer, right-click Shared and click Paste
  25. Right-click _Layout - Copy.cshtml and click Rename
  26. Type _Management to get _Management.cshtml, and press Enter
  27. Double-click _Management.cshtml to open it
  28. Change the document as follows:
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Apartment Rental Management :: @ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Apartment Rental Management", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("Employees", "Index", "Employees", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("Apartments", "Index", "Apartments", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("Rent Contracts", "Index", "RentalContracts", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("Payments", "Index", "Payments", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("About ARM", "About", "Home", new { area = "" }, null)</li>
                        <li>@Html.ActionLink("Contact Us", "Contact", "Home", new { area = "" }, null)</li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p class="text-center common-font blue">&copy; @DateTime.Now.Year - Apartment Rental Management</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
  29. In the Solution Explorer, expand Controllers and double-click HomeController
  30. Change the document as follows:
    using System.Web.Mvc;
    
    namespace ApartmentsRentalManagement1.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Title = "Home Page";
    
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Title = "About Us";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Title = "Contact Us";
    
                return View();
            }
    
            public ActionResult CentralPoint()
            {
                ViewBag.Title = "Central Point";
    
                return View();
            }
        }
    }
  31. In the document, righ-click CentralPoint() and click Add View...
  32. In the central list of the Add New Scaffolded Item dialog box, make sure MVC 5 View is selected

    Entity Data Model Wizard

    Click Add
  33. In the Add View dialog box, make sure the View Name text box is displaying CentralPoint.
    On the right side of the Use A Layout Page text box, click the Browse button Object Browser
  34. In the Project Folders tree list, expand Views and click Shared
  35. In the Contents of Folder list, click _Management.cshtml:

    Select a Layout Page

  36. Click OK
  37. On the Add View dialog box, click Add

The Database

To store the records necessary for apartments rental, we will create a Microsoft SQL Server database. We will then use the Entity Framework (EF) to connect to the data from our webpages.

Practical LearningPractical Learning: Creating a Collection

  1. To create a database:
    • If you will use a Microsoft SQL Server Database:
      1. Start SQL Server Management Studio and login
      2. Right-click the name of the computer and click New Query
      3. Type the following code:
        USE master;
        GO
        CREATE DATABASE ApartmentsRentalManagement2;
        GO
        USE ApartmentsRentalManagement2;
        GO
      4. To create the database, right-click inside the Query window and click Execute
      5. Press Ctrl + A to select everything in the Query Editor and press Delete
    • If you will use a local database:
      1. In the Solution Explorer of Microsoft Visual Studio, right-click App_Data -> Add -> New Item...
      2. In the middle frame of the Add New Item dialog box, click SQL Server Database.
        Change the Name to ApartmentsRentalManagement2
      3. Click Add
      4. In the Solution Explorer, under App_Data, right-click ApartmentsRentalManagement2 and click Open
      5. In the Server Explorer, right-click ApartmentsRentalManagement2 and click New Query
  2. In both cases, type the following code:
    CREATE TABLE Employees
    (
    	EmployeeId	    INT IDENTITY(1, 1),
    	EmployeeNumber  NVARCHAR(10),
    	FirstName       NVARCHAR(20),
    	LastName        NVARCHAR(20),
    	EmploymentTitle	NVARCHAR(50), 
    	CONSTRAINT PK_Employees PRIMARY KEY(EmployeeId)
    );
    GO
    CREATE TABLE Apartments
    (
    	ApartmentId	    INT IDENTITY(1, 1),
    	UnitNumber	    NVARCHAR(5),
    	Bedrooms	    NVARCHAR(3),
    	Bathrooms	    NVARCHAR(3),
    	MonthlyRate	    NVARCHAR(6),
    	SecurityDeposit	NVARCHAR(6),
    	OccupancyStatus NVARCHAR(25),
    	CONSTRAINT PK_Apartments PRIMARY KEY(ApartmentId)
    );
    GO
    CREATE TABLE RentalContracts
    (
    	RentalContractId INT IDENTITY(1, 1),
    	ContractNumber	 NVARCHAR(10),
    	EmployeeNumber   NVARCHAR(10),
    	ContractDate	 NVARCHAR(50),
    	FirstName		 NVARCHAR(20),
    	LastName		 NVARCHAR(20),
    	MaritalStatus    NVARCHAR(25),
    	NumberOfChildren NVARCHAR(3),
    	UnitNumber	     NVARCHAR(6),
    	RentStartDate	 NVARCHAR(50),
    	CONSTRAINT PK_RentalContracts  PRIMARY KEY(RentalContractId)
    );
    GO
    CREATE TABLE Payments
    (
    	PaymentId	     INT IDENTITY(1, 1),
    	ReceiptNumber    NVARCHAR(10),
    	EmployeeNumber   NVARCHAR(10),
    	ContractNumber	 NVARCHAR(10),
    	PaymentDate      NVARCHAR(50),
    	Amount	         NVARCHAR(6),
    	Notes	         NVARCHAR(MAX),
    	CONSTRAINT PK_Payments    PRIMARY KEY(PaymentId)
    );
    GO
  3. To execute, on the SQL Editor toolbar, click the Execute button Execute
  4. Close the Query window
  5. When asked whether you want to save, click No
  6. In the Solution Explorer, right-click Models -> Add -> New Item...
  7. In the left list of the Add New Items dialog box, click Data and, in the middle list, click ADO.NET Entity Data Model (or make sure it is selected)
  8. Change the file Name to RentManagementModel
  9. Click Add
  10. In the first page of the Entity Data Model Wizard, click Code First From Database

    Entity Data Model Wizard

  11. Click Next
  12. In the second page of the wizard:
    • If you created your local database in Microsoft Visual Studio, make sure the combo box is displaying ApartmentsRentalManagement2
    • If you are using a database created in Microsoft SQL Server:
      1. Click the New Connection button
      2. Use the Microsoft SQL Server option in the Data Source list box of the Choose Data Source dialog box to locate and select the database you created
  13. Click Next
  14. Click the check box on the Tables in t the checked list box

    Entity Data Model Wizard

  15. Click Finish
  16. On the main menu, click Debug -> Start Without Debugging

Employees

As is the case in every business, employees take care of daily interactions with potential customers and current tenants. Our database uses a table of employees for that purpose.

Practical LearningPractical Learning: Creating Employees

  1. In the Solution Explorer, under Models, double-click Employee.cs
  2. Change the class as follows:
    namespace ApartmentsRentalManagement1.Models
    {
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
    
        [Table("HumanResources.Employees")]
        public partial class Employee
        {
            [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 = "Employment Title")]
            public string EmploymentTitle { get; set; }
    
            public string Identification
            {
                get
                {
                    return EmployeeNumber + " - " + FirstName + " " + LastName + " (" + EmploymentTitle + ")";
                }
            }
        }
    }
  3. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  4. In the middle list of the Add New Scaffolded Item dialog box, click MVC 5 Controller With Views, Using Entity Framework

    Add Scaffold

  5. Click Add
  6. In the Model Class combo box, select Employee (ApartmentsRentalManagement1.Models)
  7. In the Data Context combo box, select RentManagementModel (ApartmentsRentalManagement1.Models)
  8. Make sure the Use A Layout Page text box is displaying ~/Views/Shared/_Management.cshtml.
  9. Make sure the Controller Name combo box is displaying EmployeesController

    Add Controller

  10. Click Add
  11. In the class, right-click Index() and click Go To View
  12. Change the webpage as follows:
    @model IEnumerable<ApartmentsRentalManagement1.Models.Employee>
    
    @{
        ViewBag.Title = "Employees";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Employees - Staff Members</h2>
    
    <table class="table table-hover common-font">
        <tr>
            <th class="text-center">@Html.DisplayNameFor(model => model.EmployeeId)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.EmployeeNumber)</th>
            <th>@Html.DisplayNameFor(model => model.FirstName)</th>
            <th>@Html.DisplayNameFor(model => model.LastName)</th>
            <th>@Html.DisplayNameFor(model => model.EmploymentTitle)</th>
            <th>@Html.ActionLink("Hire New Employee", "Create")</th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td class="text-center">@Html.DisplayFor(modelItem => item.EmployeeId)</td>
            <td class="text-center">@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>
                @Html.ActionLink("Edit", "Edit", new { id = item.EmployeeId }) ::
                @Html.ActionLink("Details", "Details", new { id = item.EmployeeId }) ::
                @Html.ActionLink("Delete", "Delete", new { id = item.EmployeeId })
            </td>
        </tr>
    }
    </table>
  13. In the Solution Explorer, under Views, and under Employees, double-click Details.cshtml
  14. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Employee
    
    @{
        ViewBag.Title = "Employee Details";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Employee Details</h2>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.EmployeeId)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.FirstName)</dt>
            <dd>@Html.DisplayFor(model => model.FirstName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.LastName)</dt>
            <dd>@Html.DisplayFor(model => model.LastName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmploymentTitle)</dt>
            <dd>@Html.DisplayFor(model => model.EmploymentTitle)</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit/Update Employee Record", "Edit", new { id = Model.EmployeeId }) ::
        @Html.ActionLink("Delete Employee Record", "Delete", new { id = Model.EmployeeId }) ::
        @Html.ActionLink("Employees", "Index")
    </p>
  15. In the Solution Explorer, under Views and under Employees, double-click Create.cshtml
  16. Change the form as follows:
    @model ApartmentsRentalManagement1.Models.Employee
    
    @{
        ViewBag.Title = "Create Employee";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Employment Application</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="containment">
        <div class="form-horizontal common-font">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.EmployeeNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmploymentTitle, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.EmploymentTitle, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmploymentTitle, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Employees", "Index")
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Save Employee Record" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
  17. In the Solution Explorer, under Views and under Employees, double-click Edit.cshtml
  18. Change the webpage as follows:
    @model ApartmentsRentalManagement1.Models.Employee
    
    @{
        ViewBag.Title = "Edit Employee";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class=" bold blue common-font text-center">Edit/Update Employee Information</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="containment">
        <div class="form-horizontal common-font">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.EmployeeId)
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.EmployeeNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmploymentTitle, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.EmploymentTitle, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmploymentTitle, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Employees", "Index")
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update Employee Record" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
  19. In the Solution Explorer, under Views and under Employees, click Delete.cshtml
  20. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Employee
    
    @{
        ViewBag.Title = "Delete Employee";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Deleting Employee Record</h2>
    
    <hr />
    
    <h3 class="common-font blue text-center">Are you sure you want to delete this employee's record?</h3>
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.EmployeeId)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.FirstName)</dt>
            <dd>@Html.DisplayFor(model => model.FirstName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.LastName)</dt>
            <dd>@Html.DisplayFor(model => model.LastName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmploymentTitle)</dt>
            <dd>@Html.DisplayFor(model => model.EmploymentTitle)</dd>
        </dl>
    
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
    
            <div class="form-actions no-color">
                <input type="submit" value="Delete Employee Record" class="btn btn-primary" /> ::
                @Html.ActionLink("Employees", "Index")
            </div>
        }
    </div>

Apartments

Apartments are the main objects of our business. We will use a table to keep track of their description and status.

Practical LearningPractical Learning: Creating Apartments

  1. In the Solution Explorer, under Models, double-click Apartment.cs
  2. Change the class as follows:
    namespace ApartmentsRentalManagement1.Models
    {
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
    
        [Table("Management.Apartments")]
        public partial class Apartment
        {
            [Display(Name = "Apartment Id")]
            public int ApartmentId { get; set; }
    
            [StringLength(5)]
            [Display(Name = "Unit #")]
            public string UnitNumber { get; set; }
    
            [StringLength(3)]
            public string Bedrooms { get; set; }
    
            [StringLength(3)]
            public string Bathrooms { get; set; }
    
            [StringLength(6)]
            [Display(Name = "Monthly Rate")]
            public string MonthlyRate { get; set; }
    
            [StringLength(6)]
            [Display(Name = "Deposit")]
            public string SecurityDeposit { get; set; }
    
            [StringLength(25)]
            [Display(Name = "Occupancy Status")]
            public string OccupancyStatus { get; set; }
    
            public string Residence
            {
                get
                {
                    string beds = Bedrooms + " bedrooms";
                    string baths = Bathrooms + " bathrooms";
    
                    if (Bedrooms == "1")
                        beds = Bedrooms + " bedroom";
                    if (Bathrooms == "1")
                        baths = Bedrooms + " bathroom";
    
                    return UnitNumber + " - " + beds + ", " + baths + ", rent = " +
                           MonthlyRate + "/month, deposit = " +
                           SecurityDeposit + ", " + OccupancyStatus;
                }
            }
        }
    }
  3. In the Solution Explorer, right-click Controllers -> Add -> New Scaffolded Item...
  4. In the left list of the Add New Scaffolded Item dialog box, under Common, click MVC and, in the middle list, click MVC 5 Controller With Views, Using Entity Framework
  5. press Enter
  6. In the Model Class, select Apartment (ApartmentsRentalManagement1.Models)
  7. Make sure the Data Context Class is displaying RentManagementModel (ApartmentsRentalManagement1.Models).
    Make sure the Use A Layout Page is displaying ~/Views/Shared/_Management.cshtml.
    Make sure the Controller Name text box is displaying ApartmentsController.
    Click Add
  8. Change the controller class as follows:
    using ApartmentsRentalManagement1.Models;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web.Mvc;
    
    namespace ApartmentsRentalManagement1.Controllers
    {
        public class ApartmentsController : Controller
        {
            private RentManagementModel db = new RentManagementModel();
    
            // GET: Apartments
            public ActionResult Index()
            {
                return View(db.Apartments.ToList());
            }
    
            // GET: Apartments/Details/5
            public ActionResult Details(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Apartment apartment = db.Apartments.Find(id);
                if (apartment == null)
                {
                    return HttpNotFound();
                }
                return View(apartment);
            }
    
            // GET: Apartments/Create
            public ActionResult Create()
            {
                List<SelectListItem> conditions = new List<SelectListItem>();
    
                conditions.Add(new SelectListItem() { Text = "Unknown", Value = "Unknown" });
                conditions.Add(new SelectListItem() { Text = "Occupied", Value = "Occupied" });
                conditions.Add(new SelectListItem() { Text = "Available", Value = "Available" });
                conditions.Add(new SelectListItem() { Text = "Not Ready", Value = "Not Ready" });
                conditions.Add(new SelectListItem() { Text = "Needs Maintenance", Value = "Needs Maintenance" });
    
                ViewData["OccupancyStatus"] = conditions;
    
                return View();
            }
    
            // POST: Apartments/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "ApartmentId,UnitNumber,Bedrooms,Bathrooms,MonthlyRate,SecurityDeposit,OccupancyStatus")] Apartment apartment)
            {
                if (ModelState.IsValid)
                {
                    db.Apartments.Add(apartment);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(apartment);
            }
    
            // GET: Apartments/Edit/5
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Apartment apartment = db.Apartments.Find(id);
                if (apartment == null)
                {
                    return HttpNotFound();
                }
    
                List<SelectListItem> conditions = new List<SelectListItem>
                {
                    new SelectListItem() { Text = "Unknown",           Value = "Unknown",           Selected = (apartment.OccupancyStatus == "Unknown")           },
                    new SelectListItem() { Text = "Occupied",          Value = "Occupied",          Selected = (apartment.OccupancyStatus == "Occupied")          },
                    new SelectListItem() { Text = "Available",         Value = "Available",         Selected = (apartment.OccupancyStatus == "Available")         },
                    new SelectListItem() { Text = "Not Ready",         Value = "Not Ready",         Selected = (apartment.OccupancyStatus == "Not Ready")         },
                    new SelectListItem() { Text = "Needs Maintenance", Value = "Needs Maintenance", Selected = (apartment.OccupancyStatus == "Needs Maintenance") }
                };
    
                ViewData["OccupancyStatus"] = conditions;
    
                return View(apartment);
            }
    
            // POST: Apartments/Edit/5
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "ApartmentId,UnitNumber,Bedrooms,Bathrooms,MonthlyRate,SecurityDeposit,OccupancyStatus")] Apartment apartment)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(apartment).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(apartment);
            }
    
            // GET: Apartments/Delete/5
            public ActionResult Delete(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Apartment apartment = db.Apartments.Find(id);
                if (apartment == null)
                {
                    return HttpNotFound();
                }
                return View(apartment);
            }
    
            // POST: Apartments/Delete/5
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            public ActionResult DeleteConfirmed(int id)
            {
                Apartment apartment = db.Apartments.Find(id);
                db.Apartments.Remove(apartment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }
  9. In the class, right-click Index() and click Go To View
  10. Change the document as follows:
    @model IEnumerable<ApartmentsRentalManagement1.Models.Apartment>
    
    @{
        ViewBag.Title = "Apartments";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Apartments</h2>
    
    <table class="table table-hover common-font">
        <tr>
            <th class="text-center">@Html.DisplayNameFor(model => model.ApartmentId)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.UnitNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.Bedrooms)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.Bathrooms)</th>
            <th>@Html.DisplayNameFor(model => model.MonthlyRate)</th>
            <th>@Html.DisplayNameFor(model => model.SecurityDeposit)</th>
            <th>@Html.DisplayNameFor(model => model.OccupancyStatus)</th>
            <th>@Html.ActionLink("New Apartment", "Create")</th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td class="text-center">@Html.DisplayFor(modelItem => item.ApartmentId)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.UnitNumber)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.Bedrooms)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.Bathrooms)</td>
            <td>@Html.DisplayFor(modelItem => item.MonthlyRate)</td>
            <td>@Html.DisplayFor(modelItem => item.SecurityDeposit)</td>
            <td>@Html.DisplayFor(modelItem => item.OccupancyStatus)</td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.ApartmentId }) ::
                @Html.ActionLink("Details", "Details", new { id = item.ApartmentId }) ::
                @Html.ActionLink("Delete", "Delete", new { id = item.ApartmentId })
            </td>
        </tr>
    }
    </table>
  11. In the Solution Explorer, under Views and under Apartments, double-click Details.cshtml
  12. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Apartment
    
    @{
        ViewBag.Title = "Apartment Details";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Apartment Details</h2>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.ApartmentId)</dt>
            <dd>@Html.DisplayFor(model => model.ApartmentId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.UnitNumber)</dt>
            <dd>@Html.DisplayFor(model => model.UnitNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Bedrooms)</dt>
            <dd>@Html.DisplayFor(model => model.Bedrooms)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Bathrooms)</dt>
            <dd>@Html.DisplayFor(model => model.Bathrooms)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.MonthlyRate)</dt>
            <dd>@Html.DisplayFor(model => model.MonthlyRate)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.SecurityDeposit)</dt>
            <dd>@Html.DisplayFor(model => model.SecurityDeposit)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.OccupancyStatus)</dt>
            <dd>@Html.DisplayFor(model => model.OccupancyStatus)</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit Apartment Details", "Edit", new { id = Model.ApartmentId }) ::
        @Html.ActionLink("Delete this Apartment", "Delete", new { id = Model.ApartmentId }) ::
        @Html.ActionLink("Apartments", "Index")
    </p>
  13. In the Solution Explorer, under Views and under Apartments, double-click Create.cshtml
  14. Change the form as follows:
    @model ApartmentsRentalManagement1.Models.Apartment
    
    @{
        ViewBag.Title = "Create Apartment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Apartment Setup</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="containment">
        <div class="form-horizontal common-font">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.UnitNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.UnitNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UnitNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Bedrooms, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Bedrooms, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Bedrooms, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Bathrooms, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Bathrooms, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Bathrooms, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.MonthlyRate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.MonthlyRate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.MonthlyRate, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.SecurityDeposit, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.SecurityDeposit, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SecurityDeposit, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.OccupancyStatus, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.DropDownList("OccupancyStatus", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.OccupancyStatus, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Apartments", "Index")
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Create Apartment Record" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
  15. In the Solution Explorer, under Views and under Apartments, double-click Edit.cshtml
  16. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Apartment
    
    @{
        ViewBag.Title = "Edit Apartment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Change Apartment Setup</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="containment">
        <div class="form-horizontal common-font">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.ApartmentId)
    
            <div class="form-group">
                @Html.LabelFor(model => model.UnitNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.UnitNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UnitNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Bedrooms, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Bedrooms, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Bedrooms, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Bathrooms, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Bathrooms, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Bathrooms, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.MonthlyRate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.MonthlyRate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.MonthlyRate, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.SecurityDeposit, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.SecurityDeposit, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SecurityDeposit, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.OccupancyStatus, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.DropDownList("OccupancyStatus", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.OccupancyStatus, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Apartments", "Index")
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update Apartment Setup" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
  17. In the Solution Explorer, under Views and under Apartments, double-click Delete.cshtml
  18. Change the code as follows:
    @model ApartmentsRentalManagement1.Models.Apartment
    
    @{
        ViewBag.Title = "Delete Apartment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Delete Apartment</h2>
    
    <h3 class="common-font blue text-center">Are you sure you want to remove this apartment from our system?</h3>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.ApartmentId)</dt>
            <dd>@Html.DisplayFor(model => model.ApartmentId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.UnitNumber)</dt>
            <dd>@Html.DisplayFor(model => model.UnitNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Bedrooms)</dt>
            <dd>@Html.DisplayFor(model => model.Bedrooms)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Bathrooms)</dt>
            <dd>@Html.DisplayFor(model => model.Bathrooms)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.MonthlyRate)</dt>
            <dd>@Html.DisplayFor(model => model.MonthlyRate)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.SecurityDeposit)</dt>
            <dd>@Html.DisplayFor(model => model.SecurityDeposit)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.OccupancyStatus)</dt>
            <dd>@Html.DisplayFor(model => model.OccupancyStatus)</dd>
        </dl>
    
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
    
            <div class="form-actions no-color">
                <input type="submit" value="Delete this Apartment" class="btn btn-primary" /> ::
                @Html.ActionLink("Apartments", "Index")
            </div>
        }
    </div>

Rent Contracts

Tenants are people who rent apartments. Before they take possession of an apartment, they must have an account that contain information as a contract. That's why we will use a table for tenant registration.

Practical LearningPractical Learning: Creating Rental Contracts

  1. In the Solution Explorer, under Models, double-click RentContract.cs
  2. Change the class as follows:
    namespace ApartmentsRentalManagement1.Models
    {
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
    
        [Table("Management.RentalContracts")]
        public partial class RentalContract
        {
            [Display(Name = "Rent Contract Id")]
            public int RentalContractId { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Contract #")]
            public string ContractNumber { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Employee #")]
            public string EmployeeNumber { get; set; }
    
            [StringLength(50)]
            [Display(Name = "Contract Date")]
            public string ContractDate { 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(25)]
            [Display(Name = "Marital Status")]
            public string MaritalStatus { get; set; }
    
            [StringLength(3)]
            [Display(Name = "Children")]
            public string NumberOfChildren { get; set; }
    
            [StringLength(6)]
            [Display(Name = "Unit #")]
            public string UnitNumber { get; set; }
    
            [StringLength(50)]
            [Display(Name = "Rent Start Date")]
            public string RentStartDate { get; set; }
    
            public string Description
            {
                get
                {
                    return ContractNumber + " - " + FirstName + " " + LastName + " (renting since " + RentStartDate + ")";
                }
            }
        }
    }
  3. In the Solution Explorer, right-click ApartmentsRentalManagement1 -> Add -> New Folder
  4. Type RentControllers as the name of the folder
  5. In the Solution Explorer, right-click RentControllers -> Add -> Controller...
  6. In the left list of the Add New Scaffolded Item dialog box, click Web API
  7. In the middle list of the Add New Scaffolded Item dialog box, click Web API 2 Controller With Actions, Using Entity Framework

    Add New Scaffolded Item - Web API 2 Controller With Actions, Using Entity Framework

  8. Click Add
  9. In the Model Class combo box, select Apartment (ApartmentsRentalManagement1.Models)

    Add Controller

  10. Press Enter
  11. In the Solution Explorer, right-click RentControllers -> Add -> New Scaffolded Item...
  12. In the left list of the Add New Scaffolded Item dialog box, under Common, click Web API and, in the middle frame, click Web API 2 Controller With Actions, Using Entity Framework
  13. Click Add
  14. In the Model Class combo box, select Employee (ApartmentsRentalManagement1.Models)
  15. Click Add
  16. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  17. In the middle list of the Add New Scaffolded Item dialog box, click MVC 5 Controller With Views, Using Entity Framework
  18. Click Add
  19. In the Model Class combo box, select RentalContract (ApartmentsRentalManagement1.Models)
  20. Make sure the Data Context combo box is displaying RentManagementModel (ApartmentsRentalManagement1.Models).
    Make sure the Use a Layout Page text box is displaying ~/Views/Shared/_Management.cshtml.
    Click Add
  21. Change the class as follows:
    using ApartmentsRentalManagement1.Models;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web.Mvc;
    
    namespace ApartmentsRentalManagement1.Controllers
    {
        public class RentalContractsController : Controller
        {
            private RentManagementModel db = new RentManagementModel();
    
            // GET: RentalContracts
            public ActionResult Index()
            {
                return View(db.RentalContracts.ToList());
            }
    
            // GET: RentalContracts/Details/5
            public ActionResult Details(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                RentalContract rentalContract = db.RentalContracts.Find(id);
                if (rentalContract == null)
                {
                    return HttpNotFound();
                }
                return View(rentalContract);
            }
    
            // GET: RentalContracts/Create
            public ActionResult Create()
            {
                List<SelectListItem> maritals = new List<SelectListItem>
                {
                    new SelectListItem() { Text = "Unknown",   Value = "Unknown"   },
                    new SelectListItem() { Text = "Single",    Value = "Single"    },
                    new SelectListItem() { Text = "Widdow",    Value = "Widdow"    },
                    new SelectListItem() { Text = "Married",   Value = "Married"   },
                    new SelectListItem() { Text = "Divorced",  Value = "Divorced"  },
                    new SelectListItem() { Text = "Separated", Value = "Separated" }
                };
    
                ViewBag.MaritalStatus = maritals;
    
                return View();
            }
    
            // POST: RentalContracts/Create
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "RentalContractId,ContractNumber,EmployeeNumber,ContractDate,FirstName,LastName,MaritalStatus,NumberOfChildren,UnitNumber,RentStartDate")] RentalContract rentalContract)
            {
                if (ModelState.IsValid)
                {
                    db.RentalContracts.Add(rentalContract);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(rentalContract);
            }
    
            // GET: RentalContracts/Edit/5
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                RentalContract rentalContract = db.RentalContracts.Find(id);
                if (rentalContract == null)
                {
                    return HttpNotFound();
                }
    
                List<SelectListItem> maritals = new List<SelectListItem>
                {
                    new SelectListItem() { Text = "Single",    Value = "Single",    Selected = (rentalContract.MaritalStatus == "Single")    },
                    new SelectListItem() { Text = "Widdow",    Value = "Widdow",    Selected = (rentalContract.MaritalStatus == "Widdow")    },
                    new SelectListItem() { Text = "Married",   Value = "Married",   Selected = (rentalContract.MaritalStatus == "Married")   },
                    new SelectListItem() { Text = "Unknown",   Value = "Unknown",   Selected = (rentalContract.MaritalStatus == "Unknown")   },
                    new SelectListItem() { Text = "Divorced",  Value = "Divorced",  Selected = (rentalContract.MaritalStatus == "Divorced")  },
                    new SelectListItem() { Text = "Separated", Value = "Separated", Selected = (rentalContract.MaritalStatus == "Separated") }
                };
    
                ViewBag.MaritalStatus = maritals;
                return View(rentalContract);
            }
    
            // POST: RentalContracts/Edit/5
            // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
            // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include = "RentalContractId,ContractNumber,EmployeeNumber,ContractDate,FirstName,LastName,MaritalStatus,NumberOfChildren,UnitNumber,RentStartDate")] RentalContract rentalContract)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(rentalContract).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(rentalContract);
            }
    
            // GET: RentalContracts/Delete/5
            public ActionResult Delete(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                RentalContract rentalContract = db.RentalContracts.Find(id);
                if (rentalContract == null)
                {
                    return HttpNotFound();
                }
                return View(rentalContract);
            }
    
            // POST: RentalContracts/Delete/5
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            public ActionResult DeleteConfirmed(int id)
            {
                RentalContract rentalContract = db.RentalContracts.Find(id);
                db.RentalContracts.Remove(rentalContract);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }
  22. In the class, right-click below Index() and click Go To View
  23. Change the document as follows:
    @model IEnumerable<ApartmentsRentalManagement1.Models.RentalContract>
    
    @{
        ViewBag.Title = "Rent Contracts";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Rent Contracts</h2>
    
    <table class="table table-hover common-font">
        <tr>
            <th class="text-center">@Html.DisplayNameFor(model => model.RentalContractId)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.ContractNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.EmployeeNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.ContractDate)</th>
            <th>@Html.DisplayNameFor(model => model.FirstName)</th>
            <th>@Html.DisplayNameFor(model => model.LastName)</th>
            <th>@Html.DisplayNameFor(model => model.MaritalStatus)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.NumberOfChildren)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.UnitNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.RentStartDate)</th>
            <th class="text-center">@Html.ActionLink("Process Rent Contract", "Create")</th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td class="text-center">@Html.DisplayFor(modelItem => item.RentalContractId)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.ContractNumber)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.EmployeeNumber)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.ContractDate)</td>
            <td>@Html.DisplayFor(modelItem => item.FirstName)</td>
            <td>@Html.DisplayFor(modelItem => item.LastName)</td>
            <td>@Html.DisplayFor(modelItem => item.MaritalStatus)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.NumberOfChildren)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.UnitNumber)</td>
            <td class="text-center">@Html.DisplayFor(modelItem => item.RentStartDate)</td>
            <td class="text-center">
                @Html.ActionLink("Edit", "Edit", new { id = item.RentalContractId }) :: 
                @Html.ActionLink("Details", "Details", new { id = item.RentalContractId }) :: 
                @Html.ActionLink("Delete", "Delete", new { id = item.RentalContractId })
            </td>
        </tr>
    }
    </table>
  24. In the Solution Explorer, under Views and under RentalContracts, double-click Details.cshtml
  25. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.RentalContract
    
    @{
        ViewBag.Title = "Rent Contract Details";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Rent Contract Details</h2>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.RentalContractId)</dt>
            <dd>@Html.DisplayFor(model => model.RentalContractId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ContractNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ContractNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ContractDate)</dt>
            <dd>@Html.DisplayFor(model => model.ContractDate)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.FirstName)</dt>
            <dd>@Html.DisplayFor(model => model.FirstName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.LastName)</dt>
            <dd>@Html.DisplayFor(model => model.LastName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.MaritalStatus)</dt>
            <dd>@Html.DisplayFor(model => model.MaritalStatus)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.NumberOfChildren)</dt>
            <dd>@Html.DisplayFor(model => model.NumberOfChildren)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.UnitNumber)</dt>
            <dd>@Html.DisplayFor(model => model.UnitNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.RentStartDate)</dt>
            <dd>@Html.DisplayFor(model => model.RentStartDate)</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit", "Edit", new { id = Model.RentalContractId }) ::
        @Html.ActionLink("Delete this Rent Contract", "Delete", new { id = Model.RentalContractId }) :: 
        @Html.ActionLink("Rent Contracts", "Index")
    </p>
  26. In the Solution Explorer, under Views and under RentalContracts, double-click Create.cshtml
  27. Create the form as follows:
    @model ApartmentsRentalManagement1.Models.RentalContract
    
    @{
        ViewBag.Title = "Create Rent Contract";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Process New Rent Contract</h2>
    
    <hr />
    
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        
    <div class="form-horizontal common-font">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.ContractNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("EmployeeNumber", null, htmlAttributes: new { @class = "form-control empl-number" })
                @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Employee Details</label>
            <div class="col-md-7">
                @Html.TextBox("EmployeeDetails", null, 
                              htmlAttributes: new { @class = "form-control employee-details", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.ContractDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.ContractDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractDate, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.MaritalStatus, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.DropDownList("MaritalStatus", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MaritalStatus, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.NumberOfChildren, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.NumberOfChildren, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NumberOfChildren, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.UnitNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("UnitNumber", null, htmlAttributes: new { @class = "form-control unit-number" })
                @Html.ValidationMessageFor(model => model.UnitNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Apartment Details</label>
            <div class="col-md-7">
                @Html.TextArea("ApartmentDetails", null, 
                               htmlAttributes: new { @class = "form-control unit-details", rows = "4", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.RentStartDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.RentStartDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RentStartDate, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5">
                @Html.ActionLink("Rent Contracts", "Index")
            </label>
            <div class="col-md-7">
                <input type="submit" value="Save Rent Contract" class="btn btn-primary" />
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    
    <script type="text/javascript">
        $(document).ready(function () {
            $(".empl-number").blur(function (event) {
                $.ajax({
                    method: "GET",
                    url: "/api/Employees",
                    success: function (data) {
                        $.each(data, function (index, staff) {
                            if (staff["EmployeeNumber"] === $(".empl-number").val()) {
                                $(".employee-details").val(staff["FirstName"] + " " + staff["LastName"] + " (" + staff["EmploymentTitle"] + ")");
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
            }); // blur
    
            $(".unit-number").blur(function (event) {
                $.ajax({
                    method: "GET",
                    url: "/api/Apartments",
                    success: function (data) {
                        $.each(data, function (index, apart) {
                            if (apart["UnitNumber"] === $(".unit-number").val()) {
                                $(".unit-details").val(apart["Residence"]);
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
            }); // blur
        }); // $(document)
    </script>
    }
  28. In the Solution Explorer, under Views and under RentalContracts, double-click Edit.cshtml
  29. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.RentalContract
    
    @{
        ViewBag.Title = "Edit Rent Contract";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Edit/Update Rent Contract</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="form-horizontal common-font">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.RentalContractId)
    
        <div class="form-group">
            @Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.ContractNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("EmployeeNumber", null, htmlAttributes: new { @class = "form-control empl-number" })
                @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Employee Details</label>
            <div class="col-md-7">
                @Html.TextBox("EmployeeDetails", null, htmlAttributes: new { @class = "form-control employee-details", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.ContractDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.ContractDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractDate, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.MaritalStatus, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.DropDownList("MaritalStatus", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.MaritalStatus, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.NumberOfChildren, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.NumberOfChildren, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NumberOfChildren, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.UnitNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("UnitNumber", null, htmlAttributes: new { @class = "form-control unit-number" })
                @Html.ValidationMessageFor(model => model.UnitNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Apartment Details</label>
            <div class="col-md-7">
                @Html.TextArea("ApartmentDetails", null,
                               htmlAttributes: new { @class = "form-control unit-details", rows = "4", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.RentStartDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.RentStartDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RentStartDate, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5">
                @Html.ActionLink("Rent Contracts", "Index")
            </label>
            <div class="col-md-7">
                <input type="submit" value="Save Rent Contract" class="btn btn-primary" />
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    
        <script type="text/javascript">
            $(document).ready(function () {
                $.ajax({
                    method: "GET",
                    url: "/api/Employees",
                    success: function (data) {
                        $.each(data, function (index, staff) {
                            if (staff["EmployeeNumber"] === $(".empl-number").val()) {
                                $(".employee-details").val(staff["FirstName"] + " " + staff["LastName"] + " (" + staff["EmploymentTitle"] + ")");
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
    
                $.ajax({
                    method: "GET",
                    url: "/api/Apartments",
                    success: function (data) {
                        $.each(data, function (index, apart) {
                            if (apart["UnitNumber"] === $(".unit-number").val()) {
                                $(".unit-details").val(apart["Residence"]);
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
    
            $(".empl-number").blur(function (event) {
                $.ajax({
                    method: "GET",
                    url: "/api/Employees",
                    success: function (data) {
                        $.each(data, function (index, staff) {
                            if (staff["EmployeeNumber"] === $(".empl-number").val()) {
                                $(".employee-details").val(staff["FirstName"] + " " + staff["LastName"] + " (" + staff["EmploymentTitle"] + ")");
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
            }); // blur
    
            $(".unit-number").blur(function (event) {
                $.ajax({
                    method: "GET",
                    url: "/api/Apartments",
                    success: function (data) {
                        $.each(data, function (index, apart) {
                            if (apart["UnitNumber"] === $(".unit-number").val()) {
                                $(".unit-details").val(apart["Residence"]);
                            } // if
                        }); // $.each function
                    } // success function
                }); // $.ajax
            }); // blur
        }); // $(document)
        </script>
    }
  30. In the Solution Explorer, under Views and under RentalContracts, double-click Delete.cshtml
  31. Change the code as follows:
    @model ApartmentsRentalManagement1.Models.RentalContract
    
    @{
        ViewBag.Title = "Delete Rent Contract";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Delete Rent Contract</h2>
    
    <hr />
    
    <h3 class="common-font blue text-center">Are you sure you want to delete or cancel this rent contract?</h3>
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.RentalContractId)</dt>
            <dd>@Html.DisplayFor(model => model.RentalContractId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ContractNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ContractNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ContractDate)</dt>
            <dd>@Html.DisplayFor(model => model.ContractDate)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.FirstName)</dt>
            <dd>@Html.DisplayFor(model => model.FirstName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.LastName)</dt>
            <dd>@Html.DisplayFor(model => model.LastName)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.MaritalStatus)</dt>
            <dd>@Html.DisplayFor(model => model.MaritalStatus)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.NumberOfChildren)</dt>
            <dd>@Html.DisplayFor(model => model.NumberOfChildren)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.UnitNumber)</dt>
            <dd>@Html.DisplayFor(model => model.UnitNumber)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.RentStartDate)</dt>
            <dd>@Html.DisplayFor(model => model.RentStartDate)</dd>
        </dl>
    
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
    
            <div class="form-actions no-color">
                <input type="submit" value="Delete/Cancel Rent Contract" class="btn btn-primary" /> :: 
                @Html.ActionLink("Rent Contracts", "Index")
            </div>
        }
    </div>

Rent Payments

Tenants are asked to pay rent at the end of every month. To manage this aspect of the business, we had created a table. Now we need visual objects that the user can use to create and manage payments records.

Practical LearningPractical Learning: Managing Rent Payments

  1. In the Solution Explorer, under Models, double-click Payment.cs
  2. Change the class as follows:
    namespace ApartmentsRentalManagement1.Models
    {
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
    
        [Table("Management.Payments")]
        public partial class Payment
        {
            [Display(Name = "Payment Id")]
            public int PaymentId { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Receipt #")]
            public string ReceiptNumber { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Employee #")]
            public string EmployeeNumber { get; set; }
    
            [StringLength(10)]
            [Display(Name = "Contract #")]
            public string ContractNumber { get; set; }
    
            [StringLength(50)]
            [Display(Name = "Payment Date")]
            public string PaymentDate { get; set; }
    
            [StringLength(6)]
            public string Amount { get; set; }
    
            public string Notes { get; set; }
        }
    }
  3. In the Solution Explorer, right-click RentControllers -> Add -> New Scaffolded Item...
  4. In the middle list of the Add New Scaffolded Item dialog box, click Web API 2 Controller With Actions, Using Entity Framework
  5. Click Add
  6. In the Model Class combo box, select RentalContract (ApartmentsRentalManagement1.Models)
  7. Click Add
  8. In the Solution Explorer, right-click Controllers -> Add -> Controller...
  9. In the middle frame of the Add New Scaffolded Item dialog box, click MVC 5 Controller With Views, Usin Entity Framework
  10. Click Add
  11. In the Model Class combo box, select Payment (ApartmentsRentalManagement1.Models)
  12. Make sure the Use A Layout Page text box displays ~/Views/Shared/_Management.cshtml.
    Click Add
  13. In the class, right-click Index() and click Go To View
  14. In the Add View dialog box, make sure the View Name text box displays Index
  15. Change the document as follows:
    @model IEnumerable<ApartmentsRentalManagement1.Models.Payment>
    
    @{
        ViewBag.Title = "Rent Payments";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Rent Payments</h2>
    
    <table class="table table-hover common-font">
        <tr>
            <th class="text-center">@Html.DisplayNameFor(model => model.PaymentId)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.ReceiptNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.EmployeeNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.ContractNumber)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.PaymentDate)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.Amount)</th>
            <th class="text-center">@Html.DisplayNameFor(model => model.Notes)</th>
            <th class="text-center">@Html.ActionLink("Process Rent Contract", "Create")</th>
        </tr>
    
        @foreach (var item in Model)
        {
    <tr>
        <td class="text-center">@Html.DisplayFor(modelItem => item.PaymentId)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.ReceiptNumber)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.EmployeeNumber)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.ContractNumber)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.PaymentDate)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.Amount)</td>
        <td class="text-center">@Html.DisplayFor(modelItem => item.Notes)</td>
        <td class="text-center">
            @Html.ActionLink("Edit", "Edit", new { id = item.PaymentId }) ::
            @Html.ActionLink("Details", "Details", new { id = item.PaymentId }) ::
            @Html.ActionLink("Delete", "Delete", new { id = item.PaymentId })
        </td>
    </tr>
        }
    </table>
  16. In the Solution Explorer, under Views and under Payments, double-click Details.cshtml
  17. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Payment
    
    @{
        ViewBag.Title = "Payment Details";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold blue common-font text-center">Payment Details</h2>
    
    <hr />
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.PaymentId)</dt>
            <dd>@Html.DisplayFor(model => model.PaymentId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ReceiptNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ReceiptNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ContractNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ContractNumber)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.PaymentDate)</dt>
            <dd>@Html.DisplayFor(model => model.PaymentDate)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Amount)</dt>
            <dd>@Html.DisplayFor(model => model.Amount)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.Notes)</dt>
            <dd>@Html.DisplayFor(model => model.Notes)</dd>
        </dl>
    </div>
    
    <p class="text-center">
        @Html.ActionLink("Edit", "Edit", new { id = Model.PaymentId }) ::
        @Html.ActionLink("Delete this Rent Payment", "Delete", new { id = Model.PaymentId }) ::
        @Html.ActionLink("Rent Payments", "Index")
    </p>
  18. In the Solution Explorer, under Views and under Payments, double-click Create.cshtml
  19. Transform the form as follows:
    @model ApartmentsRentalManagement1.Models.Payment
    
    @{
        ViewBag.Title = "Create Payment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Rent Payment</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    <div class="form-horizontal common-font">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ReceiptNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.ReceiptNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ReceiptNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("EmployeeNumber", null, htmlAttributes: new { @class = "form-control empl-number" })
                @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Employee Details</label>
            <div class="col-md-7">
                @Html.TextBox("EmployeeDetails", null,
                              htmlAttributes: new { @class = "form-control employee-details", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.TextBox("ContractNumber", null, htmlAttributes: new { @class = "form-control contract-number" })
                @Html.ValidationMessageFor(model => model.ContractNumber, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Contract Details</label>
            <div class="col-md-7">
                @Html.TextArea("ContractDetails", null,
                               htmlAttributes: new { @class = "form-control contract-details", rows = "3", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5 blue">Apartment Details</label>
            <div class="col-md-7">
                @Html.TextArea("ApartmentDetails", null,
                               htmlAttributes: new { @class = "form-control unit-details", rows = "2", disabled = "disabled" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.PaymentDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.PaymentDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PaymentDate, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-5 blue" })
            <div class="col-md-7">
                @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
            </div>
        </div>
    
        <div class="form-group">
            <label class="control-label col-md-5">
                @Html.ActionLink("Rent Payments", "Index")
            </label>
            <div class="col-md-7">
                <input type="submit" value="Save Rent Payment" class="btn btn-primary" />
            </div>
        </div>
    </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    
        <script type="text/javascript">
            $(document).ready(function () {
                $(".empl-number").blur(function (event) {
                    $.ajax({
                        method: "GET",
                        url: "/api/Employees",
                        success: function (data) {
                            $.each(data, function (index, staff) {
                                if (staff["EmployeeNumber"] === $(".empl-number").val()) {
                                    $(".employee-details").val(staff["FirstName"] + " " + staff["LastName"] + " (" + staff["EmploymentTitle"] + ")");
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
                }); // blur
    
                var unitNbr = "";
    
                $(".contract-number").blur(function (event) {
                    $.ajax({
                        method: "GET",
                        url: "/api/RentalContracts",
                        success: function (data) {
                            $.each(data, function (index, engagement) {
                                if (engagement["ContractNumber"] === $(".contract-number").val()) {
                                    $(".contract-details").val("Contract # " + engagement["ContractNumber"] + " for " + engagement["FirstName"] + " " + engagement["LastName"] + " (" + engagement["MaritalStatus"] + ", " + engagement["NumberOfChildren"] + " child(dren)); rents Unit #" + engagement["UnitNumber"] + " since " + engagement["RentStartDate"] + ".");
    
                                    unitNbr = engagement["UnitNumber"];
    
                                    // $(".contract-details").val(engagement["Description"]);
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
    
                    $.ajax({
                        method: "GET",
                        url: "/api/Apartments",
                        success: function (data) {
                            $.each(data, function (index, apart) {
                                if (apart["UnitNumber"] === unitNbr) {
                                    $(".unit-details").val(apart["Residence"]);
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
                }); // blur
            }); // $(document)
        </script>
    }
  20. Click the PaymentsController.cs tab to access the controller
  21. In the class, right-click Edit() and click Go To View
  22. Change the form as follows:
    @model ApartmentsRentalManagement1.Models.Payment
    
    @{
        ViewBag.Title = "Edit Payment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Edit Rent Payment</h2>
    
    <hr />
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal common-font">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.PaymentId)
    
            <div class="form-group">
                @Html.LabelFor(model => model.ReceiptNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.ReceiptNumber, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ReceiptNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.TextBox("EmployeeNumber", null, htmlAttributes: new { @class = "form-control empl-number" })
                    @Html.ValidationMessageFor(model => model.EmployeeNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5 blue">Employee Details</label>
                <div class="col-md-7">
                    @Html.TextBox("EmployeeDetails", null,
                                  htmlAttributes: new { @class = "form-control employee-details", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.TextBox("ContractNumber", null, htmlAttributes: new { @class = "form-control contract-number" })
                    @Html.ValidationMessageFor(model => model.ContractNumber, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5 blue">Contract Details</label>
                <div class="col-md-7">
                    @Html.TextArea("ContractDetails", null,
                                   htmlAttributes: new { @class = "form-control contract-details", rows = "3", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5 blue">Apartment Details</label>
                <div class="col-md-7">
                    @Html.TextArea("ApartmentDetails", null,
                                   htmlAttributes: new { @class = "form-control unit-details", rows = "2", disabled = "disabled" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.PaymentDate, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.PaymentDate, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.PaymentDate, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-5 blue" })
                <div class="col-md-7">
                    @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <label class="control-label col-md-5">
                    @Html.ActionLink("Rent Payments", "Index")
                </label>
                <div class="col-md-7">
                    <input type="submit" value="Update Rent Payment" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    
        <script type="text/javascript">
            $(document).ready(function () {
                $(".empl-number").blur(function (event) {
                    $.ajax({
                        method: "GET",
                        url: "/api/Employees",
                        success: function (data) {
                            $.each(data, function (index, staff) {
                                if (staff["EmployeeNumber"] === $(".empl-number").val()) {
                                    $(".employee-details").val(staff["FirstName"] + " " + staff["LastName"]
                                                                                  + " (" + staff["EmploymentTitle"] + ")");
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
                }); // blur
    
                var unitNbr = "";
    
                $(".contract-number").blur(function (event) {
                    $.ajax({
                        method: "GET",
                        url: "/api/RentalContracts",
                        success: function (data) {
                            $.each(data, function (index, engagement) {
                                if (engagement["ContractNumber"] === $(".contract-number").val()) {
                                    $(".contract-details").val("Contract # " + engagement["ContractNumber"] + " for "
                                                                             + engagement["FirstName"] + " " + engagement["LastName"]
                                                                             + " (" + engagement["MaritalStatus"] + ", "
                                                                             + engagement["NumberOfChildren"]
                                                                             + " child(dren)); rents Unit #" + engagement["UnitNumber"]
                                                                             + " since " + engagement["RentStartDate"] + ".");
    
                                    unitNbr = engagement["UnitNumber"];
    
                                    // $(".contract-details").val(engagement["Description"]);
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
    
                    $.ajax({
                        method: "GET",
                        url: "/api/Apartments",
                        success: function (data) {
                            $.each(data, function (index, apart) {
                                if (apart["UnitNumber"] === unitNbr) {
                                    $(".unit-details").val(apart["Residence"]);
                                } // if
                            }); // $.each function
                        } // success function
                    }); // $.ajax
                }); // blur
            }); // $(document)
        </script>
    }
  23. Click the PaymentsController.cs tab to access the controller
  24. In the class, right-click Delete() and click Go To View
  25. Change the document as follows:
    @model ApartmentsRentalManagement1.Models.Payment
    
    @{
        ViewBag.Title = "Delete Payment";
        Layout = "~/Views/Shared/_Management.cshtml";
    }
    
    <h2 class="bold common-font blue text-center">Delete Rent Payment</h2>
    
    <hr />
    
    <h3 class="common-font blue text-center">Are you sure you want to delete this rent payment?</h3>
    
    <div class="containment">
        <dl class="dl-horizontal common-font">
            <dt>@Html.DisplayNameFor(model => model.PaymentId)</dt>
            <dd>@Html.DisplayFor(model => model.PaymentId)</dd>
    
            <dt>@Html.DisplayNameFor(model => model.ReceiptNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ReceiptNumber)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.EmployeeNumber)</dt>
            <dd>@Html.DisplayFor(model => model.EmployeeNumber)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.ContractNumber)</dt>
            <dd>@Html.DisplayFor(model => model.ContractNumber)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.PaymentDate)</dt>
            <dd>@Html.DisplayFor(model => model.PaymentDate)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.Amount)</dt>
            <dd>@Html.DisplayFor(model => model.Amount)</dd>
            
            <dt>@Html.DisplayNameFor(model => model.Notes)</dt>
            <dd>@Html.DisplayFor(model => model.Notes)</dd>
        </dl>
    
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
    
            <div class="form-actions no-color">
                <input type="submit" value="Delete Rent Payment" class="btn btn-primary" /> ::
                @Html.ActionLink("Rent Payments", "Index")
            </div>
        }
    </div>

Finalizing the Application

Close the following tabs: _Layout.cshtml, Index.cshtml, Delete.cshtml. Edit.cshtml, Delete.cshtml, Employee.cs, Apartment.cs, RentalContract.cs, Payment.cs, EmployeesController.cs, and BundleConfig.cs

Practical LearningPractical Learning: Finalizing the Application

  1. In the Solution Explorer, under Views, expand Home, and double-click Index.cshtml
  2. In the document, delete one of the <p> lines below the jumbotron format. Here is an example:
    @{
        ViewBag.Title = "Home Page";
    }
    
    <div class="jumbotron">
        <h1>ASP.NET</h1>
        <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    </div>
    
    <div class="row">
        <div class="col-md-4">
            <h2>Getting started</h2>
            <p>
                ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
                enables a clean separation of concerns and gives you full control over markup
                for enjoyable, agile development.
            </p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
        </div>
        <div class="col-md-4">
            <h2>Get more libraries</h2>
            <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
        </div>
        <div class="col-md-4">
            <h2>Web Hosting</h2>
            <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
            <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
        </div>
    </div>
  3. To execute, on the main menu, click Debug -> Start Without Debugging

    Joins Fundamentals

  4. In the webpage, click RENT MANAGEMENT
  5. Click the Employees link

    Joins Fundamentals

  6. In the webpage, click Hire New Employee

    Joins Fundamentals

  7. Create the following records:
     
    Employee # First Name Last Name Title
    93947 Catherine Watts Owner - General Manager
    40685 Justine Sandt Rent Manager
    73048 Raymond Wilkinson Intern
    60949 Mark Reason Maintenance Technician
    38408 Marc Knights Rent Associate
    20448 Nancy Longhorn Rent Associate
    INSERT INTO HumanResources.Employees(EmployeeNumber, FirstName,LastName, EmploymentTitle)
    VALUES(N'93947', N'Catherine', N'Watts',	   N'Owner - General Manager'),
    	    (N'40685', N'Justine',	 N'Sandt',	   N'Rent Manager'),
    	    (N'73048', N'Raymond',	 N'Wilkinson', N'Intern'),
    	    (N'60949', N'Mark', 	 N'Reason',	   N'Maintenance Technician'),
    	    (N'38408', N'Marc', 	 N'Knights',   N'Rent Associate'),
    	    (N'20448', N'Nancy',	 N'Longhorn',  N'Rent Associate');
    GO

    Joins Fundamentals

  8. In the Solution Explorer, under Views and under Apartments, double-click Index.cshtml to access it
  9. To execute, on the main menu, click Debug -> Start Without Debugging
  10. Click the New Apartment link

    Joins Fundamentals

  11. Create the following records:
     
    Unit # Bedrooms Bathrooms Monthly Rate Security Deposit Occupancy Status
    1012213501100Available
    102111150 850Needs Maintenance
    103111150 850Available
    1043215001250Available
    1052112501000 Not Ready
    1063215501250Available
    1073214501250Needs Maintenance
    108111100 850Available
    1092213501100Available
    110111050 850 Not Ready
    1112213501100Needs Maintenance
    1122112851000Available
    2012111851000 Not Ready
    202111150 850Available
    203111150 850Available
    2043216001250Available
    2052111001000Needs Maintenance
    2063215001250Available
    2073215501250Available
    20811 985 850Available
    2092213501100Available
    210111150 850 Not Ready
    2112213501100Available
    2122110751000Available
    3012211751000Available
    302111150 850Needs Maintenance
    303111100 850Available
    3043212501100Available
    3052111001000Available
    3063212501100Available
    3073211001250Available
    308111100 850Available
    309221100 950Available
    310111100 850Available
    3112211001000 Not Ready
    3122111001000Available
    INSERT INTO Management.Apartments(UnitNumber, Bedrooms, Bathrooms, MonthlyRate, SecurityDeposit, OccupancyStatus)
    VALUES(N'101', 2, 2, 1350, 1100, N'Available'),
          (N'102', 1, 1, 1150,  850, N'Needs Maintenance'),
          (N'103', 1, 1, 1150,  850, N'Available'),
          (N'104', 3, 2, 1500, 1250, N'Available'),
          (N'105', 2, 1, 1250, 1000, N'Not Ready'),
          (N'106', 3, 2, 1550, 1250, N'Available'),
          (N'107', 3, 2, 1450, 1250, N'Needs Maintenance'),
          (N'108', 1, 1, 1100,  850, N'Available'),
          (N'109', 2, 2, 1350, 1100, N'Available'),
          (N'110', 1, 1, 1050,  850, N'Unknown'),
          (N'111', 2, 2, 1350, 1100, N'Needs Maintenance'),
          (N'112', 2, 1, 1285, 1000, N'Available'),
          (N'201', 2, 1, 1185, 1000, N'Unknown'),
          (N'202', 1, 1, 1150,  850, N'Available'),
          (N'203', 1, 1, 1150,  850, N'Available'),
          (N'204', 3, 2, 1600, 1250, N'Available'),
          (N'205', 2, 1, 1100, 1000, N'Needs Maintenance'),
          (N'206', 3, 2, 1500, 1250, N'Available'),
          (N'207', 3, 2, 1550, 1250, N'Available'),
          (N'208', 1, 1,  985,  850, N'Available'),
          (N'209', 2, 2, 1350, 1100, N'Available'),
          (N'210', 1, 1, 1150,  850, N'Unknown'),
          (N'211', 2, 2, 1350, 1100, N'Available'),
          (N'212', 2, 1, 1075, 1000, N'Available'),
          (N'301', 2, 2, 1175, 1000, N'Available'),
          (N'302', 1, 1, 1150,  850, N'Needs Maintenance'),
          (N'303', 1, 1, 1100,  850, N'Available'),
          (N'304', 3, 2, 1250, 1100, N'Available'),
          (N'305', 2, 1, 1100, 1000, N'Needs Maintenance'),
          (N'306', 3, 2, 1250, 1100, N'Available'),
          (N'307', 3, 2, 1100, 1250, N'Available'),
          (N'308', 1, 1, 1100,  850, N'Available'),
          (N'309', 2, 2, 1100,  950, N'Available'),
          (N'310', 1, 1, 1100,  850, N'Available'),
          (N'311', 2, 2, 1100, 1000, N'Unknown'),
          (N'312', 2, 1, 1100, 1000, N'Available');
    GO

    Joins Fundamentals

  12. Click the Rent Contracts link

    Joins Fundamentals

  13. Click the Start New Rent Contract link

    Joins Fundamentals

  14. Create the following records:

    Joins Fundamentals

    Contract # Processed By Contract Date First Name Last Name Marital Status # of Children Unit # Rent Start Date
    1001 38408 6/12/2022 Ann Sanders Married 1 109 7/1/2022
    1002 20448 6/15/2022 Mahty Shaoul   2 104 9/1/2022
    1003 40685 6/22/2022 Frank Ulm Single 0 302 7/1/2022
    1004 93947 6/22/2022 Elise Provowski Separated 1 305 8/1/2022
    1005 93947 7/23/2022 Grace Curryan   1 105 9/1/2022
    1006 38408 7/25/2022 Tracy Warrens Divorced 2 307 8/1/2022
    1007 38408 8/1/2022 Paul Yamo Married 3 204 10/1/2022
    1008 40685 8/10/2022 Nancy Shermann Single 1 108 9/1/2022
    1009 20448 9/12/2022 Michael Tiernan   0 209 11/1/2022
    1010 38408 10/5/2022 Phillippe Anderson Single 0 202 11/1/2022
    INSERT INTO Management.RentalContracts(ContractNumber,	EmployeeNumber,	ContractDate,	FirstName,	LastName,	MaritalStatus,	NumberOfChildren,	UnitNumber, RentStartDate)
    VALUES(1001,	N'38408',	N'6/12/2020',	N'Ann',			N'Sanders',		N'Married',		1,	9,	N'7/1/2020'),
          (1002,	N'20448',	N'6/15/2020',	N'Mahty',		N'Shaoul',		NULL,			2,	4,	N'9/1/2020'),
          (1003,	N'40685',	N'6/22/2020',	N'Frank',		N'Ulm',			N'Single',		0,	3,	N'7/1/2020'),
          (1004,	N'93947',	N'6/22/2020',	N'Elise',		N'Provowski',	N'Separated',	1,	29,	N'8/1/2020'),
          (1005,	N'93947',	N'7/23/2020',	N'Grace',		N'Curryan',		NULL,			1,	5,	N'9/1/2020'),
          (1006,	N'38408',	N'7/25/2020',	N'Tracy',		N'Warrens',		N'Divorced',	2,	31,	N'8/1/2020'),
          (1007,	N'38408',	N'8/1/2020',	N'Paul',		N'Yamo',		N'Married',		3,	16,	N'10/1/2020'),
          (1008,	N'40685',	N'8/10/2020',	N'Nancy',		N'Shermann',	N'Single',		1,	8,	N'9/1/2020'),
          (1009,	N'20448',	N'9/12/2020',	N'Michael',		N'Tiernan',		NULL,			0,	21,	N'11/1/2020'),
          (1010,	N'38408',	N'10/5/2020',	N'Phillippe',	N'Anderson',	N'Single',		0,	14,	N'11/1/2020');
    GO

    Joins Fundamentals

  15. Click the Payments link

    Joins Fundamentals

  16. Click the New Rent Payment link

    Joins Fundamentals

  17. Create the following records:
     
    Receipt # Employee # Contract # Payment Date Amount Notes
    100001 20448 1002 06/15/2022 1250 This is the payment of the security deposit.
    100002 38408 1001 06/17/2022 1100 This is the first payment of the tenant. It is for the security deposit.
    100003 40685 1004 06/22/2022 1000 Security deposit payment.
    100004 93947 1003 06/25/2022 850 This was the security deposit payment
    100005 40685 1003 07/26/2022 1100 This was the July 2018 rent payment
    100006 38408 1001 07/31/2022 1350 Rent payment for July 2022
    100007 40685 1004 08/25/2022 1100 August 2018 rent payment
    100008 20448 1003 08/30/2022 1100 August 2018 rent payment
    100009 20448 1001 08/31/2022 1350 Rent payment for August 2022
    100010 93947 1002 09/26/2022 1500 Rent payment - September 2022
    100011 38408 1003 09/27/2022 1100 Rent payment for September 2022
    100012 93947 1001 09/28/2022 1350 This is the rent payment for September 2022
    100013 20448 1004 09/29/2022 1100 September 2018 monthly payment
    100014 93947 1003 10/28/2022 1100 October 2018 Rent
    100015 20448 1002 10/29/2022 1500 This was the payment for October 2022
    INSERT INTO Management.Payments(ReceiptNumber,	EmployeeNumber,	ContractNumber,	PaymentDate, Amount, Notes)
    VALUES(100001,	N'20448',	1002,	N'06/15/2020',	1250,	N'This is the payment of the security deposit'),
          (100002,	N'38408',	1001,	N'06/17/2020',	1100,	N'This is the payment of the security deposit'),
          (100003,	N'40685',	1004,	N'06/22/2020',	1000,	N'Security deposit payment'),
          (100004,	N'93947',	1003,	N'06/25/2020',	 850,	N'This was the security deposit payment'),
          (100005,	N'40685',	1003,	N'07/26/2020',	1100,	N'This was the July 2018 rent payment'),
          (100006,	N'38408',	1001,	N'07/31/2020',	1350,	N'Rent payment for July 2018'),
          (100007,	N'40685',	1004,	N'08/25/2020',	1100,	N'August 2018 rent payment'),
          (100008,	N'20448',	1003,	N'08/30/2020',	1100,	N'August 2018 rent payment'),
          (100009,	N'20448',	1001,	N'08/31/2020',	1350,	N'Rent payment for August 2018'),
          (100010,	N'93947',	1002,	N'09/26/2020',	1500,	N'Rent payment - September 2018'),
          (100011,	N'38408',	1003,	N'09/27/2020',	1100,	N'Rent payment for September 2018'),
          (100012,	N'93947',	1001,	N'09/28/2020',	1350,	N'This is the rent payment for September 2018'),
          (100013,	N'20448',	1004,	N'09/29/2020',	1100,	N'September 2018 monthly payment'),
          (100014,	N'93947',	1003,	N'10/28/2020',	1100,	N'October 2018 Rent'),
          (100015,	N'20448',	1002,	N'10/29/2020',	1500,	N'This was the payment for October 2018'),
          (100016,	N'93947',	1001,	N'10/30/2020',	1350,	N'Rent for October 2018'),
          (100017,	N'38408',	1004,	N'10/30/2020',	1100,	N'Rent payment for October 2018'),
          (100018,	N'20448',	1001,	N'11/26/2020',	1350,	N'November 2018 Rent'),
          (100019,	N'40685',	1003,	N'11/29/2020',	1100,	N'November 2018 Rent Payment'),
          (100020,	N'20448',	1002,	N'11/30/2020',	1500,	N'This is the rent payment for November 2018'),
          (100021,	N'40685',	1004,	N'11/30/2020',	1100,	N'Rent payment for November 2018'),
          (100022,	N'93947',	1002,	N'12/27/2020',	1500,	N'Rent payment - December 2018'),
          (100023,	N'20448',	1004,	N'12/30/2020',	1100,	N'December 2018 rent payment'),
          (100024,	N'38408',	1003,	N'12/31/2020',	1100,	N'Rent payment for December 2018'),
          (100025,	N'38408',	1001,	N'01/04/2021',	1350,	N'This was rent for December 2018'),
          (100026,	N'40685',	1004,	N'01/20/2021',	1100,	N'January 2019 rent payment'),
          (100027,	N'38408',	1003,	N'01/26/2021',	1100,	N'January 2019 rent payment'),
          (100028,	N'38408',	1002,	N'01/28/2021',	1500,	N'January 2019 - Rent Payment'),
          (100029,	N'20448',	1001,	N'02/02/2021',	1350,	N'This was rent for January 2018'),
          (100030,	N'38408',	1004,	N'02/25/2021',	1100,	N'February 2019 rent payment'),
          (100031,	N'20448',	1003,	N'02/26/2021',	1100,	N'Rent payment for February 2021'),
          (100032,	N'20448',	1002,	N'02/27/2021',	1500,	N'February 2019 Rent'),
          (100033,	N'38408',	1001,	N'03/01/2021',	1350,	N'Rent for February 2021'),
          (100034,	N'40685',	1003,	N'03/27/2021',	1100,	N'Rent payment for March 2021'),
          (100035,	N'20448',	1004,	N'03/29/2021',	1100,	N'March 2019 rent payment'),
          (100036,	N'38408',	1002,	N'03/30/2021',	1500,	N'Rent payment for March 2021'),
          (100037,	N'38408',	1001,	N'03/31/2021',	1350,	N'March 2018 Rent');
    GO

    Joins Fundamentals

  18. Close the browser and return to your programming environment
  19. Close your programming environment

Home Copyright © 2007-2022, FunctionX Wednesday 04 May 2022 Home