Question How to modify action details to get data from another model ?

ahmedaziz

Well-known member
Joined
Feb 22, 2023
Messages
55
Programming Experience
1-3
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 Application/GetAllDetails I will get all Details contain ID AND DetailsName from model details where Header Id=1

details models

Details model:
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; }

     }

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();

      

    }
 
In general, you don't modify the controller action. The controller action should simply talk to your repository (or database context in the case of EF), and that repository should return the required object (e.g. the model). The controller shouldn't be so tightly coupled to your data access layer that it knows all the nitty-gritty details. If it is tightly coupled, then you'll be in the position where any changes you make down in the model or data access layer (for example you decide to implement some caching, or you split an the data storage behind an entity into two separate tables) will require you to do a code review of all your controller actions to make sure that you don't break anything.
 
Back
Top Bottom