TblBook.cs
public class TBLBook : IEntity
{
[Key]
public int BookId { get; set; }
public string BookName { get; set; }
[ForeignKey("TblCategory")]
public int CategoryId { get; set; }
public int WriterId { get; set; }
public string BookYearofPublication { get; set; }//Bas?m Y?l?
public string BookPublishingHouse { get; set; } //Yay?n evi
public string BookPage { get; set; }
public bool BookStatus { get; set; }
public TBLCategory TblCategory { get; set; }
}
TblCategory.cs
public class TBLCategory : IEntity
{
[Key]
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<TBLBook> TblBooks { get; set; }
}
I did this because it is corporate architecture.
public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
where TEntity : class, IEntity, new()
where TContext : DbContext, new()
{
////return _context.Categories.Include(i => i.SubCategories).ToList();
public List<TEntity> GetList(Expression<Func<TEntity, bool>> filter=null)
{
using (TContext context = new TContext())
{
return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList();
}
}
}
Business:
public List<TBLBook> GetList()
{
return _bookDal.GetList();
}
Controller View
public IActionResult Index()
{
var query = new BookListViewModel
{
TblBooks = _bookService.GetList()
};
return View(query);
}
Index View:
<tr>
<td>@b.BookId</td>
<td>@b.BookName</td>
<td>@b.WriterId</td>
<td>@b.CategoryId</td> <!--Problem: @b.TblCategory.CategoryName-->
<td>@b.BookYearofPublication</td>
<td>@b.BookPublishingHouse</td>
<td>@b.BookPage</td>
<td>@b.BookStatus</td>
</tr>
My problem is;
when i do this
@b.TblCategories.CategoryName
throws null or error.
I cannot show the corresponding category name. how can I fill it, thanks.
My English is bad, sorry. I tried to explain with pictures.
In short, here's what I want to do:
The category ID appears. I want it to appear as the Category Name. but it looks blank, what should I fill with.
Screen-shot:
question from:
https://stackoverflow.com/questions/65902888/how-can-i-make-a-book-book-category 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…