C
C#

help

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

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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Uncaught Exception C++ hostfxr HandlingHi I have a C++ dll that I am using to host some dotnet assembly and normally things work but when sEntity Data Model WizardI was watching this tutorial: https://www.youtube.com/watch?v=1oGxPoTGl0U&ab_channel=TekTuition AroWhen to use EF queries, LinQ, or MySQLClientAttached is one of the Entity Framework (EF) queries I'm referring to (I'm not sure if this is what Process Checker [Answered]I'm creating an ""antivirus"" and I'm using a thread to check when a new process is started (but theArgumentNullException when argument is not nullI have this code: ```cs public class Config { [JsonProperty("token")] public string Token = "TOKWPF reusing the same template across multiple columnsThe following question https://social.msdn.microsoft.com/Forums/silverlight/en-US/f84e0efd-b1f2-4ab6Cascading DropDownI'm using MVC (.NET5) with EF and had this code to select a species on a View (to Create/Edit): ```cAssembly service findingHi, I am trying to find out if it's possible to get IServiceProvider, Microsoft IConfiguration or sohow do i submit a progra min vs to google classroomim trying to submit a project that's due to today but when i do submit it all i get is this and not WPF custom property not recognized by the compilerI have defined a custom property like below, it's in the namespace `CarApp`. It is supposed to repreEntity Framework DbContextOptionsBuilder.UseSqlServer() missing definitionI just installed the newest version of Entity Framework (6.0.9) and am inside my program.cs trying tDoes API manual nuget downloads come with the package dependencies?-WPF passing binding to a template from DataGridTemplateColumnSay, I have a template like this in resources, which I want to reuse for multiple columns, and whichBest way to implement a regex based lexer [Answered]In a regex lexer, you can loop over every pattern and do something like this: ```cs if (match.Successtring query in C Sharpi am trying to get values from 2 website links and assign them to a lable using visual studio one is there a convention for validating login sessions with MVC?Assuming I'm using ASP, ADO and .NET Say I want to construct a project to have several functions thUsing SQL db to create object ID or application?I have an application where my records in my DB need to have unique IDs. Should I leave this responsObject Initializers - To Parenthesize or Not To Parenthesize (that is the question)What is the difference between the object initializers on lines 1 and 2? In what situations should oHow do I move my mouse curser on screen?I went through most of stack overflow and some YouTube tutorials but didn't fins anythingHow do I move my mouse curser on screen?i was wondering if its possible to move the curse on the screen using c#