C
C#2y ago
pape

✅ How do i make this create button work?(Razor Pages,entityframework)

Ok, so i am making an inventory managment site, i connected an sql database to it and i can add stuff to it and it will show up on the website. But now i want to make it so that the user can add his own things and save them to the database, but i don't know how to hook up the button, and the stuff on the internet is not very clear to me.
47 Replies
pape
papeOP2y ago
pape
papeOP2y ago
the button sends you to new page where you will create your own entry in the database but i just don't know how to do it
Angius
Angius2y ago
The link should be working from what I see As long as you have a Dodaj page
pape
papeOP2y ago
Yeah it sends you to a new create page, that i know how to do, but im asking how do i actually input new data into my sql folder
pape
papeOP2y ago
pape
papeOP2y ago
is this alright?
pape
papeOP2y ago
blinkbat
blinkbat2y ago
Basic Save - EF Core
Basic information on adding, updating and removing data with Entity Framework Core
pape
papeOP2y ago
ok so what do i need to add to my DodajModel so that its connected to entityframework?
Angius
Angius2y ago
Inject the context How did you display data on that other page? You needed the dbcontext there
pape
papeOP2y ago
Oh so i do the same thing right
pape
papeOP2y ago
pape
papeOP2y ago
is this good injection? Sorry im new to this
Angius
Angius2y ago
Yeah
pape
papeOP2y ago
alright so i injected it into this page how do i make it so that the cshtml page adds stuff to it? the microsoft learn page is kinda confusing but im trying some methods to crack it i just dont know how to connect it with html stuff
Angius
Angius2y ago
Create a model class, create a property of that class in .cshtml.cs file, add [BindProperty] attribute to it In .cshtml, create a form, and form fields using
<input asp-for="Model.Property.Something" >
<input asp-for="Model.Property.Something" >
When form is sent, you'll get that data inside of the property you made Now you can use _context.Whatever.Add() to add stuff to the database And await _context.SaveChangesAsync() to save it
pape
papeOP2y ago
pape
papeOP2y ago
pape
papeOP2y ago
this good? now i just gotta add _context stuff so i add them on the cs page right? how would i write that out? hm maybe i shouldnt use get set i literally dont know i need help
Angius
Angius2y ago
_context.Things.Add(stuff you want to add) Things being a DbSet<Thing> property on your ApplicationDbContext
pape
papeOP2y ago
pape
papeOP2y ago
like this? it throws up an error I'm sorry if i'm being stupid.
Angius
Angius2y ago
What error? Also, don't do it in the constructor
pape
papeOP2y ago
\
Angius
Angius2y ago
Do it in some OnPostAsync() method
pape
papeOP2y ago
yeah that actually makes sense
pape
papeOP2y ago
pape
papeOP2y ago
Like this? I guess i need IActionResult so that when i click the save button it sends it to my database, but when i write it out it throws an error if i just put void it works with no error but doesnt actually save my data
Angius
Angius2y ago
Well, it's not async And you don't await your SaveChangesAsync() call
pape
papeOP2y ago
can you explain to me what "await" does?
Angius
Angius2y ago
Asynchronous programming Since calling the database takes time, the app can do some things in the meantime, while it's waiting That's the gist of what async/await does All you need to remember, is that every async method call needs to be awaited, and that you can only await inside of async methods And to never ever use async void
pape
papeOP2y ago
Alright thank you
pape
papeOP2y ago
pape
papeOP2y ago
So would it be like this then?
Angius
Angius2y ago
Angius
Angius2y ago
But yes, more or less
pape
papeOP2y ago
i get a CS0161 not all code paths return a value
Angius
Angius2y ago
Well, the return type is IActionResult And you don't return an IActionResult So
pape
papeOP2y ago
yeah i literally am more confused than i was before
Angius
Angius2y ago
You say "this method returns a thing" But then never return "a thing"
pape
papeOP2y ago
What can i return? can i redirecttopage? yeah actually i can try that now Yeah it redirects to another page now yay now the only error is the await operator that can only be used within the async method but why?
Angius
Angius2y ago
Well, I told you
pape
papeOP2y ago
Angius
Angius2y ago
Angius
Angius2y ago
Exactly
pape
papeOP2y ago
yeah i got it now thank you I got it working and want to say that you aren't aware how much i'm grateful. You spent your free time helping me and i appreciate that. ❤️
Angius
Angius2y ago
Anytime 👌
Want results from more Discord servers?
Add your server