❔ 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
Angius3y ago
Don't Use a viewmodel/DTO
Pobiega
Pobiega3y ago
The proper solution is to use a viewmodel.
BigggMoustache
BigggMoustacheOP3y ago
I glanced at the topic but I didn't understand
Pobiega
Pobiega3y 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
Angius3y 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
BigggMoustacheOP3y ago
lol so @Model EmptyClass like an actual model and not the instance from method
Angius
Angius3y 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
BigggMoustacheOP3y ago
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
Angius3y ago
Why an interface?
BigggMoustache
BigggMoustacheOP3y ago
should I just use DTO with my own class to simplify things?
Pobiega
Pobiega3y ago
yes
BigggMoustache
BigggMoustacheOP3y ago
this is what I meant by making copies earlier I figured if I'm already getting a list, why make another but okay
Pobiega
Pobiega3y ago
well lists are references you wont be copying the data
BigggMoustache
BigggMoustacheOP3y ago
oh true It just felt dumb like I should be able to use what I have
Pobiega
Pobiega3y ago
just have a public record MyViewDto(List<Whatever> MyList); dont use viewbags. they are dynamic, untyped garbage
BigggMoustache
BigggMoustacheOP3y ago
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
Pobiega3y ago
you'll need to declare the record outside your method
BigggMoustache
BigggMoustacheOP3y ago
The only time I did DTO was with Automapper tutorial xD okay
Pobiega
Pobiega3y ago
so it can be "seen" from the view
BigggMoustache
BigggMoustacheOP3y ago
Yeah duh
Pobiega
Pobiega3y ago
look at ZZZZs post above
BigggMoustache
BigggMoustacheOP3y ago
well also one more question (I will scroll back up in one second
BigggMoustache
BigggMoustacheOP3y ago
They have a Card model in the package, but it has other interface / class dependencies inside of it
BigggMoustache
BigggMoustacheOP3y ago
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
Angius3y 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
BigggMoustacheOP3y ago
right
Angius
Angius3y 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
BigggMoustacheOP3y ago
yes and ICard referenced earlier, that ICardServices gets lists of
BigggMoustache
BigggMoustacheOP3y ago
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
BigggMoustacheOP3y ago
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
Pobiega3y ago
dont be afraid of making classes 🙂
BigggMoustache
BigggMoustacheOP3y ago
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.
Want results from more Discord servers?
Add your server