I am working on a project that users can add/edit/delete a book from the database and reserve it. Basically a library database. In my user table(TblUser) I have userId(PK), name, email etc.. In my book table(TblBook) I have bookID(PK), Bookname, author etc. And Finally I have the TblBookStatus which has resID(PK), bookID, userID and status. I did not connect bookID and userID as foreign key due to some project specific reasons. When a user reserves a book, the userID, bookID and status has been added as a row on TblBookStatus table. What I am trying to do is, I want to display the reserved books by a user(TblBookStatus). here is my C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LibMan.Models.DB;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
namespace LibMan.Pages
{
public class LibraryModel : PageModel
{
private readonly LibMan.Models.DB.LibManContext _context;
public string Message { get; set; }
public LibraryModel(LibMan.Models.DB.LibManContext context)
{
_context = context;
}
public IList<TblBookStatus> TblBookStatus { get; set; }
public IList<TblBook> TblBook { get; set; }
public IList<TblBook> DisplayBook { get; set; }
public async Task OnGetAsync()
{
TblBookStatus = await _context.TblBookStatus.ToListAsync();
TblBook = await _context.TblBook.ToListAsync();
foreach(var item in TblBook)
{
foreach(var item2 in TblBookStatus)
{
if(item.BookId == item2.BookId)
{
DisplayBook.Add(item);
}
}
}
}
}
}
I tried to turn both tables into lists and check for the intersecting book ID's and make a new list to display them, but I get the error : NullReferenceException: Object reference not set to an instance of an object. on the line DisplayBook.Add(item);
I have seen people writing SQL commands, but I am not sure If I should do it. Here is the HTML side
@page
@model LibMan.Pages.LibraryModel
@{
ViewData["Title"] = "Library";
Layout = "~/Pages/Shared/_postLoginLayout.cshtml";
}
<h1>Library</h1>
<p>
<a asp-page="Create">Add a new Book</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Author)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Translator)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Publisher)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Category)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Status)
</th>
<th>
@Html.DisplayNameFor(model => model.TblBook[0].Cover)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.DisplayBook)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Translator)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cover)
</td>
<td>
<a asp-page="./GetBook" asp-route-id="@item.BookId">Get Book</a> |
<a asp-page="./Edit" asp-route-id="@item.BookId">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.BookId">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.BookId">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Thanks for the help!