Skip to content

Commit

Permalink
Updated Views/Tenant/Index to have a form, the name=searchQuery match…
Browse files Browse the repository at this point in the history
…es the parameter name in the TenantController's Index action (string searchQuery). When the owner types in the search field and submits the form, the browser sends the GET method request with th input value appended as a query string, like this: /Tenant/Index?searchQuery=example. Then, the TenantController receices the value of searchQuery from the queryString, if searchQuery is not null/empty, the code: tenants = tenants.Where(t => t.email.Contains(searchQuery)); filters the list of tenants to include only those whose Email contains the value of searchQery. Next, the filtered list of tenants is converted to a List and passed to the view: return View(tenants.ToList());. Finally, the view displays the results dynamically using the filtered data.
  • Loading branch information
tuanh00 committed Nov 27, 2024
1 parent 573a002 commit 2e86397
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 63 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/prjRentalManagement/v17/.suo
Binary file not shown.
1 change: 0 additions & 1 deletion prjRentalManagement/Content/Site.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ textarea {
background-color: #c82333;
border-color: #bd2130;
}

15 changes: 12 additions & 3 deletions prjRentalManagement/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TenantController : Controller
private DbPropertyRentalEntities db = new DbPropertyRentalEntities();

// GET: Tenant
public ActionResult Index()
public ActionResult Index(string searchQuery)
{
// Redirect to Home if no owner or tenant session is active
if (Session["owner"] == null && Session["tenant"] == null)
Expand All @@ -29,10 +29,19 @@ public ActionResult Index()
// If owner is logged in, show all tenants
if (Session["owner"] != null)
{
return View(db.tenants.ToList());
// If the owner is logged in, filter tenants based on the search query
var tenants = db.tenants.AsQueryable();

if (!string.IsNullOrEmpty(searchQuery))
{
tenants = tenants.Where(t => t.email.Contains(searchQuery));
}

// Pass the filtered list to the view
return View(tenants.ToList());
}

// If a tenant is logged in, show only their details
// If a tenant is logged in, show only their information
if (Session["tenant"] != null)
{
int tenantId = Convert.ToInt32(Session["tenant"]);
Expand Down
140 changes: 81 additions & 59 deletions prjRentalManagement/Views/Tenant/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,96 @@
@{
ViewBag.Title = "Tenant Dashboard";
}

@if (Session["owner"] != null)
{
<h2>All Tenants</h2>
<div class="search-bar-container mb-3">
<form method="get" action="@Url.Action("Index", "Tenant")">
<div style="display: flex;">
<input type="text"
name="searchQuery"
class="form-control search-bar"
placeholder="Search by email..."
value="@Request.QueryString["searchQuery"]" />
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
</div>
}
else if (Session["tenant"] != null)

@if (Session["tenant"] != null)
{
<h2>Your Information</h2>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.email)
</th>
<th>
@Html.DisplayNameFor(model => model.phoneNumber)
</th>
<th>
Actions
</th>
</tr>

@if (Session["tenant"] != null)
{
foreach (var item in Model.Where(i => i.tenantId == Convert.ToInt32(Session["tenant"])))
@if (!Model.Any())
{
<!-- Message for no results -->
<p class="text-danger">No tenants found matching your search criteria.</p>
}
else
{
<!-- Table of tenants -->
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().name)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().email)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().phoneNumber)
</th>
<th>
Actions
</th>
</tr>

@if (Session["tenant"] != null)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoneNumber)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.tenantId }, new { @class = "btn btn-warning btn-sm" })
@Html.ActionLink("Details", "Details", new { id = item.tenantId }, new { @class = "btn btn-info btn-sm" })
@Html.ActionLink("Delete", "Delete", new { id = item.tenantId }, new { @class = "btn btn-danger btn-sm" })
</td>
</tr>
foreach (var item in Model.Where(i => i.tenantId == Convert.ToInt32(Session["tenant"])))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoneNumber)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.tenantId }, new { @class = "btn btn-warning btn-sm" })
@Html.ActionLink("Details", "Details", new { id = item.tenantId }, new { @class = "btn btn-info btn-sm" })
@Html.ActionLink("Delete", "Delete", new { id = item.tenantId }, new { @class = "btn btn-danger btn-sm" })
</td>
</tr>
}
}
}
else
{
foreach (var item in Model)
else
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoneNumber)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.tenantId }, new { @class = "btn btn-warning btn-sm" })
@Html.ActionLink("Details", "Details", new { id = item.tenantId }, new { @class = "btn btn-info btn-sm" })
@Html.ActionLink("Delete", "Delete", new { id = item.tenantId }, new { @class = "btn btn-danger btn-sm" })
</td>
</tr>
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoneNumber)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.tenantId }, new { @class = "btn btn-warning btn-sm" })
@Html.ActionLink("Details", "Details", new { id = item.tenantId }, new { @class = "btn btn-info btn-sm" })
@Html.ActionLink("Delete", "Delete", new { id = item.tenantId }, new { @class = "btn btn-danger btn-sm" })
</td>
</tr>
}
}
}


</table>
</table>
}
Binary file modified prjRentalManagement/bin/prjRentalManagement.dll
Binary file not shown.
Binary file modified prjRentalManagement/bin/prjRentalManagement.pdb
Binary file not shown.
Binary file modified prjRentalManagement/obj/Debug/prjRentalManagement.dll
Binary file not shown.
Binary file modified prjRentalManagement/obj/Debug/prjRentalManagement.pdb
Binary file not shown.

0 comments on commit 2e86397

Please sign in to comment.