C
C#2mo ago
aaron

anybody know why in Asp.net mvc the data is in the database but not showing up on the index

I have a order and cusotmers model. When i add a .includ customer in the order query. it jsut show tow reuslts. EVen thou there is mroe in the databse. All the relastionships are set up correctly as it is adding to data base
43 Replies
Angius
Angius2mo ago
$code
MODiX
MODiX2mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
aaron
aaron2mo ago
one sec
aaron
aaron2mo ago
No description
aaron
aaron2mo ago
No description
aaron
aaron2mo ago
hey @ZZZZZZZZZZZZZZZZZZZZZZZZZ, The probelm I have is adding the include stament include cusotmers only shows these two reuslts. Even though if you look at the pciture of the database there is alot more. Do you have any idea why this happing i have spent like 10 hours trying to fix this.
Angius
Angius2mo ago
Not sure what that .heic file is
aaron
aaron2mo ago
oh it was a image of the database
aaron
aaron2mo ago
No description
aaron
aaron2mo ago
why is the include customers braking it if i dont put that i will display everthing. The cusomter mdoel inheirtes form the built in apilalction user class but all the relastioships are set up right as it is adding to the data base @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius2mo ago
Well, it seems fine to me As in, it shoul;d be fetching all orders
aaron
aaron2mo ago
yes including cusotmers if i remove the incldue cusomter it works its shwos everhting
Angius
Angius2mo ago
What happens when you ditch the includes and use a select instead?
aaron
aaron2mo ago
do you have any idea why it is not so that would be select cusomter ill try
Angius
Angius2mo ago
No That would be selecting the data into a DTO class
aaron
aaron2mo ago
oh ok what do you mean by this
Angius
Angius2mo ago
I mean exactly and precisely what I said
var orders = await _context.Orders
.Select(o => new OrderDto {
Id = o.Id,
// ...
Status = o.Status.Name,
Customers = o.Customers.Select(c => c.Name).ToList()
})
.ToListAsync();
var orders = await _context.Orders
.Select(o => new OrderDto {
Id = o.Id,
// ...
Status = o.Status.Name,
Customers = o.Customers.Select(c => c.Name).ToList()
})
.ToListAsync();
for example
aaron
aaron2mo ago
i am trying that rn thanks
aaron
aaron2mo ago
No description
aaron
aaron2mo ago
i feel this could be the same reason the .include did not work as well could this be beacuse cusomter is iherting form the identiy user pre built in class @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius2mo ago
What is Customers? Is it a collection of customers?
aaron
aaron2mo ago
a class yes
Angius
Angius2mo ago
Why does the error imply that it is, in fact, not?
aaron
aaron2mo ago
ill show you my model
aaron
aaron2mo ago
No description
aaron
aaron2mo ago
thats in orders
aaron
aaron2mo ago
No description
Angius
Angius2mo ago
That does not look like a collection to me
No description
aaron
aaron2mo ago
oh my bad but one order can only have one cusotmer
Angius
Angius2mo ago
Why is it named plural, "Collections" then
aaron
aaron2mo ago
idk i though thats just naming convetion
Angius
Angius2mo ago
That's the worst naming convention I've seen in my entire life and fuck whoever decided to impose it on this project Regardless Since it's not a collection, we cannot use LINQ So... you just get what you need from the customer directly
var orders = await _context.Orders
.Select(o => new OrderDto {
Id = o.Id,
// ...
Status = o.Status.Name,
CustomerName = o.Customers.Name
})
.ToListAsync();
var orders = await _context.Orders
.Select(o => new OrderDto {
Id = o.Id,
// ...
Status = o.Status.Name,
CustomerName = o.Customers.Name
})
.ToListAsync();
for example
aaron
aaron2mo ago
oh so on the front end I can not be like cusotmers.name etc
Angius
Angius2mo ago
You can just fetch the whole customer, sure
aaron
aaron2mo ago
is there a way to get everthing
Angius
Angius2mo ago
But do you need to?
aaron
aaron2mo ago
ya ig becuase the admin should be able to see the cusotmers detials do i have to type each feild or is there a way to select the whole thing
Angius
Angius2mo ago
You can just do Customer = o.Customers ig
aaron
aaron2mo ago
ok ill try thank you one last thing is there a way to do it wihto a dto or is it needed since it is not a collection
Angius
Angius2mo ago
The way to do it without a DTO is the way you were doing it before Which is suitable for school projects
aaron
aaron2mo ago
but that dosent work
Angius
Angius2mo ago
But only to a point, then I'd start docking points It should work Which is why I told you to try the proper way, using a DTO If that still doesn't work, something else is broken
aaron
aaron2mo ago
ok thankk yoiu very much