Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
1.06/5 (3 votes)
I work on EF core 7 blazor . I face issue I can't call Details model from application page to get all Data for details model .

meaning I need to display details data as list on application page where Header Id = 1 on action GetAllDetails.

so when call
C#
Application/GetAllDetails
I will get all Details contain ID AND DetailsName from model details where Header Id=1

What I have tried:

details models

public class Details
{
public int ID { get; set; }
public string DetailsName { get; set; }
public int HeaderId { get; set; }
}
application model

 public class Applications
    {
        [Key]
        public int ApplicationID { get; set; }
        public string Section { get; set;}
        public string ApplicationName { get; set; }
     }
generic repository

public interface IRepository<TEntity> where TEntity : class
{
    IEnumerable<TEntity> GetAll();
}
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
    internal AppsRepositoryDBContext _context;
    internal DbSet<TEntity> dbSet;

 

       public BaseRepository(AppsRepositoryDBContext context)
        {
            _context = context;
            this.dbSet = _context.Set<TEntity>();
        }
    
        public IEnumerable<TEntity> GetAll() => _context.Set<TEntity>().ToList();
   }
on service

 public interface IapplicationService : IRepository<Applications>
    {
       
    }
public class ApplicationService : BaseRepository<Applications>, IapplicationService
    {
        private readonly AppsRepositoryDBContext _context;
        public ApplicationService(AppsRepositoryDBContext context) : base(context)
        {
            _context = context;
        }

    }
on controller application

 [Route("api/[controller]")]
    [ApiController]
    public class ApplicationController : Controller
    {
        private readonly IapplicationService _IapplicationService;
        public ApplicationController(IapplicationService iapplicationService)
        {
            _IapplicationService = iapplicationService;
        }



 [HttpGet]
    public IActionResult GetAllDetails()
    {
//How to get all details
        return Ok();
       
    }
Posted
Updated 2-Mar-23 22:23pm

1 solution

Here's an example of how you would fetch that info using the _context you already have available:

C#
[HttpGet]
public IActionResult GetAllDetails()
{
    // Retrieve all details with HeaderId = 1
    var details = _context.Details.Where(d => d.HeaderId == 1).ToList();

    // Return the list of details
    return Ok(details);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900