S
S
shvedovea2018-08-14 18:12:14
.NET
shvedovea, 2018-08-14 18:12:14

EF Core how to output dependent data?

Good evening, please.
There are 2 classes, the first class is phones, another class is models, a bunch of many to one:

public Model()
        {
            Phones = new List<Phone>();
        }

How can I display this data on the page, that is, display my own model for each phone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-08-14
@yarosroman

in controller

var model = context.Model.Include(i=>i.Phones)
return View(model)

in view
@model Model

@foreach (var phone in Model.Phones)
{

}

I
Ivan Filatov, 2018-09-09
@NYMEZIDE

1. Never mix Domain models and DTO models. Read about DDD
2. You have Model, Phone, etc. in Domain.
Return from the controller must be done through DTO simple classes with get; set properties. To do this, you need to map your domain models to DTOs and return them to the View.
At the same time, DTO objects should be made as convenient as possible for the View.
It is difficult to understand what is wrong with you and why it cannot be processed even in your case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question