mardi 26 janvier 2021

How Can I Make a Book / Book Category?

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.

Output Photo: https://i.hizliresim.com/uNmld2.png

Aucun commentaire:

Enregistrer un commentaire