❔ 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:
public async Task<IActionResult> Search(string searchString)
{
ICardService service = serviceProvider.GetCardService();
var result = await service.Where(x => x.Name, searchString).AllAsync();
ViewBag.CardsReturned = result;
return View();
}
public async Task<IActionResult> Search(string searchString)
{
ICardService service = serviceProvider.GetCardService();
var result = await service.Where(x => x.Name, searchString).AllAsync();
ViewBag.CardsReturned = result;
return View();
}
What ICardService and AllSync are:
public interface ICardService : IMtgQueryable<ICardService, CardQueryParameter>
{
//
// Summary:
// Gets all the MtgApiManager.Lib.Model.Card defined by the query parameters.
//
// Returns:
// A MtgApiManager.Lib.Core.IOperationResult`1 representing the result containing
// all the items.
Task<IOperationResult<List<ICard>>> AllAsync();

//
// Summary:
// Find a specific card by its multi verse identifier.
//
// Parameters:
// multiverseId:
// The multi verse identifier to query for.
//
// Returns:
// A MtgApiManager.Lib.Core.IOperationResult`1 representing the result containing
// a MtgApiManager.Lib.Model.Card or an exception.
public interface ICardService : IMtgQueryable<ICardService, CardQueryParameter>
{
//
// Summary:
// Gets all the MtgApiManager.Lib.Model.Card defined by the query parameters.
//
// Returns:
// A MtgApiManager.Lib.Core.IOperationResult`1 representing the result containing
// all the items.
Task<IOperationResult<List<ICard>>> AllAsync();

//
// Summary:
// Find a specific card by its multi verse identifier.
//
// Parameters:
// multiverseId:
// The multi verse identifier to query for.
//
// Returns:
// A MtgApiManager.Lib.Core.IOperationResult`1 representing the result containing
// a MtgApiManager.Lib.Model.Card or an exception.
33 Replies
Angius
Angius2y ago
Don't Use a viewmodel/DTO
Pobiega
Pobiega2y ago
The proper solution is to use a viewmodel.
BigggMoustache
I glanced at the topic but I didn't understand
Pobiega
Pobiega2y ago
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 function
Angius
Angius2y ago
1. 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. PROFIT
BigggMoustache
lol so @Model EmptyClass like an actual model and not the instance from method
Angius
Angius2y ago
var person = new Person {
Name = nameService.GetName(),
Age = ageRepository.GetAge()
};

return View(person);
var person = new Person {
Name = nameService.GetName(),
Age = ageRepository.GetAge()
};

return View(person);
@model Person

<h1>Person!</h1>
<h2>Their name is <strong>@Model.Name</strong></h2>
<h3>And they're <blink>@Model.Age</blink> years old!</h3>
@model Person

<h1>Person!</h1>
<h2>Their name is <strong>@Model.Name</strong></h2>
<h3>And they're <blink>@Model.Age</blink> years old!</h3>
BigggMoustache
when I tried to reference @model ICard I got a restriction I can't remember now I didn't make that it's a package
Angius
Angius2y ago
Why an interface?
BigggMoustache
should I just use DTO with my own class to simplify things?
Pobiega
Pobiega2y ago
yes
BigggMoustache
this is what I meant by making copies earlier I figured if I'm already getting a list, why make another but okay
Pobiega
Pobiega2y ago
well lists are references you wont be copying the data
BigggMoustache
oh true It just felt dumb like I should be able to use what I have
Pobiega
Pobiega2y ago
just have a public record MyViewDto(List<Whatever> MyList); dont use viewbags. they are dynamic, untyped garbage
BigggMoustache
lmao 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?
Pobiega
Pobiega2y ago
you'll need to declare the record outside your method
BigggMoustache
The only time I did DTO was with Automapper tutorial xD okay
Pobiega
Pobiega2y ago
so it can be "seen" from the view
BigggMoustache
Yeah duh
Pobiega
Pobiega2y ago
look at ZZZZs post above
BigggMoustache
well also one more question (I will scroll back up in one second
BigggMoustache
They have a Card model in the package, but it has other interface / class dependencies inside of it
BigggMoustache
I'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?
Angius
Angius2y ago
Generally speaking, you have one database model class, and however many DTOs you need The database class never gets exposed through the API
BigggMoustache
right
Angius
Angius2y ago
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 view
BigggMoustache
yes and ICard referenced earlier, that ICardServices gets lists of
BigggMoustache
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.
BigggMoustache
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
Pobiega
Pobiega2y ago
dont be afraid of making classes 🙂
BigggMoustache
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.
Accord
Accord2y ago
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.