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
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
How do i cast it to the correct type in the view?
Asuming 2/3 are different approaches. It wants a IEnumerable, but Im getting new errors i dont rlly understand.
You can read more details here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions
Casting and type conversions - C# Programming Guide
Learn about casting and type conversions, such as implicit, explicit (casts), and user-defined conversions.
do something similar to this
I suggest you to use List instead of IEnumerable
Its a enum tho. Idk if that changes anything
what do you mean by enum?
your data is a enum?
Cant really create a instance of = new City with a enum rt
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
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
Have you tried google about it?
it's pretty simple to load your enum to select
Yes, but they mostly give me soluations from different net versions which makes me annoyed lol. Cuz it slightley different each version zzz
could you show code that you had tried?
you're almost there
you need specify correct item in asp-items attribute
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,
...
@Html.GetEnumSelectList
this one can help you
Your city is an enum
Yeah but i cant reach the <City> in the cshtml. so i got confused
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
Didnt know i could use namespacing in cstml op.But not sure what this means. :
can you show full code of your enum City
namespace Core.Domain
{
public enum City
{
Breda,
Den_Bosch,
Tilburg
}
}
}
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
How else should i add a namespace in my view? its not @namespace Core.Domain.City?
try open _ViewImports.cshtml
it should be under Views folder
you will see an example there
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
do you know what is namespace?
no problem, I'm trying to guide you to concept
Its like the name of the class no? I would be calling that class. reading Core.Domain,City.
Organizing types in namespaces
Learn how namespaces help you organize related types.
take a short time and read this
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.
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
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? >,>
Okie. Do you know about HTTP request?
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
Okie. You haven't add parameter name for your select
And you need to catch it in controller action too
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.
you can use attribute asp-for="ParamName" .
In Controller you need to catch it. Eg: Public IActionResult CatchRequest(string ParamName)
Is visual studio tripping? or is this real. Notice some errors go away by simply restarting visual studio lol.
So with the asp-for="selectedItem", i can reach it like this. this doenst work yet ofc. but just to double check
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
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.
This should work no?
It's hard to tell
You need to debug and see the value
oof, Once clicking the different options, the debugger wont notice a change. Cant see if theres anything inside ''selectedItem''. mmm
one breakpoint in controller action should be good enough
u can see selectedItem value there
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.
"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
What you mean? Thought name="parameter", is pretty much sending the data to the controller.
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
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!
Good luck man, you still have a long road to go
Ask the thread owner or member with permission to close this!
er
you should close this thread
use /close
yeah, got 3 weeks to finish this school project zzz. but tnx np willdo.
✅ This post has been marked as answered!