❔ Viewing List from ViewBag
I'm not sure what to cast to make the
result
in ViewBag.CardsReturned
usable in the view. (Sorry if this was a load of unnecessary information)
Method creating the ViewBag:
What ICardService
and AllSync
are:
33 Replies
Don't
Use a viewmodel/DTO
The proper solution is to use a viewmodel.
I glanced at the topic but I didn't understand
buuut...
(IOperationResult<List<ICard>>)ViewBag.CardsReturned
or something should work
oh its fairly simple. You make a new class that represents the data you want to send to your view
then you just make an instance of that class, fill it with your data, and pass it to the View
function1. Create a class/record
2. Give it the values you want to pass to the view
3. Pass it to the
View()
method
4. Declare the @model
type in the view
5. ???
6. PROFITlol
so
@Model EmptyClass
like an actual model
and not the instance from methodwhen I tried to reference
@model ICard
I got a restriction I can't remember now
I didn't make that it's a packageWhy an interface?
should I just use DTO with my own class to simplify things?
yes
this is what I meant by making copies earlier
I figured if I'm already getting a list, why make another
but okay
well lists are references
you wont be copying the data
oh true
It just felt dumb
like I should be able to use what I have
just have a
public record MyViewDto(List<Whatever> MyList);
dont use viewbags. they are dynamic, untyped garbagelmao yeah so I've read.
Do I have to make like, whole copy of thing, or can I just do that one line in my method?
you'll need to declare the record outside your method
The only time I did DTO was with Automapper tutorial xD
okay
so it can be "seen" from the view
Yeah duh
look at ZZZZs post above
well also one more question (I will scroll back up in one second
They have a
Card
model in the package, but it has other interface / class dependencies inside of itI'm not sure how I would handle these idk, different layers
I'm guessing easiest would be to just make my own class, DTO what I need, and forget the rest?
Generally speaking, you have one database model class, and however many DTOs you need
The database class never gets exposed through the API
right
Not sure what's what here, but I can assume that
Card
is analogous to a database model..?
Regardless, you'd still want a DTO for your viewyes and ICard referenced earlier, that ICardServices gets lists of
Here's the github https://github.com/MagicTheGathering/mtg-sdk-dotnet/tree/bcd0e23e8e584ca3647a7a68c9fbab1e82bb6190/src/MtgApiManager.Lib/Model/Card
GitHub
mtg-sdk-dotnet/src/MtgApiManager.Lib/Model/Card at bcd0e23e8e584ca3...
Magic: The Gathering SDK - C#.NET. Contribute to MagicTheGathering/mtg-sdk-dotnet development by creating an account on GitHub.
Okay I hear you thanks for that
so no matter what I need to make another class
I'll just do exactly as you've said xD
tyvm
dont be afraid of making classes 🙂
Thanks a lot you two. Feel like I'm leveling up lately being able to navigate the litany of questions that pop up and you folks over here deserve lots of credit for it.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.