C
C#•2y ago
Tostiapparaat

How to send multiple data sources to a view in asp.net 6? [Answered]

Hey, So im learning asp.net 6. I have a page where i show products by giving a list to the view, But now i want to add a option selecter to the view based on a Enum in my model. I followed a tutorial on how to add the option selecter. But in the video it only gave the view parameter with data. How do i add Another list of data to the same view? I have 2 methods now that are called Index (Which obv dont work). But Can someone tell me how to send multiple data to the view that i can use? Since i can just use 1 @model in a view :c Hope you understand my question by looking at the code.
58 Replies
FroH.LVT
FroH.LVT•2y ago
1.You may add extra variable using ViewBag or ViewData. You will need to cast it to correct type in view 2.Extend your ViewModel class to include your "Another List of Data" so it would help you 3.Inject a service directly into your view and retrieve data using that service
Tostiapparaat
Tostiapparaat•2y ago
How do i cast it to the correct type in the view?
Tostiapparaat
Tostiapparaat•2y ago
Asuming 2/3 are different approaches. It wants a IEnumerable, but Im getting new errors i dont rlly understand.
FroH.LVT
FroH.LVT•2y ago
Casting and type conversions - C# Programming Guide
Learn about casting and type conversions, such as implicit, explicit (casts), and user-defined conversions.
FroH.LVT
FroH.LVT•2y ago
do something similar to this
// Create a new derived type.
Giraffe g = new Giraffe();

// Implicit conversion to base type is safe.
Animal a = g;

// Explicit conversion is required to cast back
// to derived type. Note: This will compile but will
// throw an exception at run time if the right-side
// object is not in fact a Giraffe.
Giraffe g2 = (Giraffe)a;
// Create a new derived type.
Giraffe g = new Giraffe();

// Implicit conversion to base type is safe.
Animal a = g;

// Explicit conversion is required to cast back
// to derived type. Note: This will compile but will
// throw an exception at run time if the right-side
// object is not in fact a Giraffe.
Giraffe g2 = (Giraffe)a;
I suggest you to use List instead of IEnumerable
Tostiapparaat
Tostiapparaat•2y ago
Its a enum tho. Idk if that changes anything
FroH.LVT
FroH.LVT•2y ago
what do you mean by enum? your data is a enum?
Tostiapparaat
Tostiapparaat•2y ago
Tostiapparaat
Tostiapparaat•2y ago
Cant really create a instance of = new City with a enum rt
FroH.LVT
FroH.LVT•2y ago
ah i see could you elaborate more details about what are you gonna do with this City enum? map it to a select in view or something
Tostiapparaat
Tostiapparaat•2y ago
Yes. i want ppl to use the select option. And whenever i add a city i can easily add a enum. and it auto updates. And evetually show new data products based of that enum.city ofc
FroH.LVT
FroH.LVT•2y ago
Have you tried google about it? it's pretty simple to load your enum to select
Tostiapparaat
Tostiapparaat•2y ago
Yes, but they mostly give me soluations from different net versions which makes me annoyed lol. Cuz it slightley different each version zzz
FroH.LVT
FroH.LVT•2y ago
could you show code that you had tried?
FroH.LVT
FroH.LVT•2y ago
FroH.LVT
FroH.LVT•2y ago
you're almost there you need specify correct item in asp-items attribute
Tostiapparaat
Tostiapparaat•2y ago
Didnt rlly find much on how to convert a viewbag item like that, So i started googling and tried older versions like this, https://stackoverflow.com/questions/388483/how-do-you-create-a-dropdownlist-from-an-enum-in-asp-net-mvc , And now i saw a @Html.GetEnumSelectList, but not sure how to use it. But if im almost there, makes me wanna fix it using the viewbag so i can use it more often lol
Stack Overflow
How do you create a dropdownlist from an enum in ASP.NET MVC?
I'm trying to use the Html.DropDownList extension method but can't figure out how to use it with an enumeration. Let's say I have an enumeration like this: public enum ItemTypes { Movie = 1, ...
FroH.LVT
FroH.LVT•2y ago
@Html.GetEnumSelectList this one can help you Your city is an enum
Tostiapparaat
Tostiapparaat•2y ago
Yeah but i cant reach the <City> in the cshtml. so i got confused
FroH.LVT
FroH.LVT•2y ago
not "Another list of data" so you don't need any method I mentioned above in this case Because you haven't add City's namespace to your view you can try use full name. Eg: MyApplication.Models.Data.City or add namespace in ViewImport
Tostiapparaat
Tostiapparaat•2y ago
Didnt know i could use namespacing in cstml op.But not sure what this means. :
FroH.LVT
FroH.LVT•2y ago
can you show full code of your enum City
Tostiapparaat
Tostiapparaat•2y ago
namespace Core.Domain { public enum City { Breda, Den_Bosch, Tilburg }
}
FroH.LVT
FroH.LVT•2y ago
as you can see your namespace is Core.Domain only and you are using namespace, not define a new one I've fixed this line, sorry my noob English. It made you confused
Tostiapparaat
Tostiapparaat•2y ago
How else should i add a namespace in my view? its not @namespace Core.Domain.City?
FroH.LVT
FroH.LVT•2y ago
try open _ViewImports.cshtml it should be under Views folder you will see an example there
Tostiapparaat
Tostiapparaat•2y ago
Tostiapparaat
Tostiapparaat•2y ago
Thats all in my viewImports. Couldnt i add the import in this folder so i can use the city everywhere in my project? And sry its like i dont understand Asp.net at all yet lol
FroH.LVT
FroH.LVT•2y ago
do you know what is namespace? no problem, I'm trying to guide you to concept
Tostiapparaat
Tostiapparaat•2y ago
Its like the name of the class no? I would be calling that class. reading Core.Domain,City.
FroH.LVT
FroH.LVT•2y ago
take a short time and read this
Tostiapparaat
Tostiapparaat•2y ago
Ahhhhh. got it! yeah silly me. It works 🙂 Thankyou lots! Incase ur bored >,> Happenly to tell me how to access this these options in my selection in my controller? What keyword tells me like ""im giving you all data in this option selecter" in the cshtml. But eitherway thankyou for the help. (: It really was simple as that. zzzz.
FroH.LVT
FroH.LVT•2y ago
It's okie. Everyone starts as a noob. You should learn about Enum type first. And add namespace then you can have full access to your City Enum in your controller
Tostiapparaat
Tostiapparaat•2y ago
I mean recieving the selected item the user picked from this <select>. asp-action"Index" brings me to the Index method in my controller, but its not showing me what the current selected item is. Happenly to know how to? >,>
FroH.LVT
FroH.LVT•2y ago
Okie. Do you know about HTTP request?
Tostiapparaat
Tostiapparaat•2y ago
Had it in class for 5 minutes but allrdy forgot about it. Ill read the docs of it if u say it will help me know how to recieve the data of this :p dont want to keep u from ur time too long lol
FroH.LVT
FroH.LVT•2y ago
Okie. You haven't add parameter name for your select And you need to catch it in controller action too
Tostiapparaat
Tostiapparaat•2y ago
Mind telling me the keyword for giving a <form> or <select> as a parameter back in the Index method? asuming i have to put something extra in the cshtml like one of those ''asp-for/items/action.
FroH.LVT
FroH.LVT•2y ago
you can use attribute asp-for="ParamName" . In Controller you need to catch it. Eg: Public IActionResult CatchRequest(string ParamName)
Tostiapparaat
Tostiapparaat•2y ago
Is visual studio tripping? or is this real. Notice some errors go away by simply restarting visual studio lol.
Tostiapparaat
Tostiapparaat•2y ago
So with the asp-for="selectedItem", i can reach it like this. this doenst work yet ofc. but just to double check
FroH.LVT
FroH.LVT•2y ago
I forgot, it's not part of your model so can use simple version: name="ParamName". I don't have good exp about Razor Syntax so it might have a better way to do that try read the error there
Tostiapparaat
Tostiapparaat•2y ago
Yeah its talking about the IEnumerable from package, thats used below this <select>. Got confused cuz the select isnt doing anything with that INumerable, assuming all it did was giving the <select> a callable parameter name. But i changed it to name= yes. But i think i have it working. now i just need to update the database colums to get it working. i think.
Tostiapparaat
Tostiapparaat•2y ago
This should work no?
FroH.LVT
FroH.LVT•2y ago
It's hard to tell You need to debug and see the value
Tostiapparaat
Tostiapparaat•2y ago
oof, Once clicking the different options, the debugger wont notice a change. Cant see if theres anything inside ''selectedItem''. mmm
FroH.LVT
FroH.LVT•2y ago
one breakpoint in controller action should be good enough u can see selectedItem value there
Tostiapparaat
Tostiapparaat•2y ago
Yeah i tried. But Its not debugging once i change the value of the the <select>. tried both ways. it wont snap back into the Index method after changing the option.
FroH.LVT
FroH.LVT•2y ago
"change the value of the the <select>" this happen in your browser. You can't catch it you need to post your data to controller
Tostiapparaat
Tostiapparaat•2y ago
What you mean? Thought name="parameter", is pretty much sending the data to the controller.
FroH.LVT
FroH.LVT•2y ago
not yet because all you do is change select value Remember when you login to any social media website? You must enter user + password then press login press login = send data
Tostiapparaat
Tostiapparaat•2y ago
aahh Yess. It updates the list of products now. Thanks alot man. (: Now i need to update the db and its done. Tnx! thanks for being patient with me!
FroH.LVT
FroH.LVT•2y ago
Good luck man, you still have a long road to go
Accord
Accord•2y ago
Ask the thread owner or member with permission to close this!
FroH.LVT
FroH.LVT•2y ago
er you should close this thread use /close
Tostiapparaat
Tostiapparaat•2y ago
yeah, got 3 weeks to finish this school project zzz. but tnx np willdo.
Accord
Accord•2y ago
✅ This post has been marked as answered!