✅ 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
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
The link should be working from what I see
As long as you have a
Dodaj
pageYeah 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
is this alright?
Basic Save - EF Core
Basic information on adding, updating and removing data with Entity Framework Core
ok so what do i need to add to my DodajModel so that its connected to entityframework?
Inject the context
How did you display data on that other page?
You needed the dbcontext there
Oh so i do the same thing right
is this good injection?
Sorry im new to this
Yeah
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
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
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 itthis 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
_context.Things.Add(stuff you want to add)
Things
being a DbSet<Thing>
property on your ApplicationDbContext
like this?
it throws up an error
I'm sorry if i'm being stupid.
What error?
Also, don't do it in the constructor
\
Do it in some
OnPostAsync()
methodyeah that actually makes sense
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
Well, it's not
async
And you don't await
your SaveChangesAsync()
callcan you explain to me what "await" does?
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 await
ed, and that you can only await
inside of async
methods
And to never ever use async void
Alright thank you
So would it be like this then?
But yes, more or less
i get a CS0161 not all code paths return a value
Well, the return type is
IActionResult
And you don't return an IActionResult
Soyeah i literally am more confused than i was before
You say "this method returns a thing"
But then never return "a thing"
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?
Well, I told you
Exactly
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. ❤️
Anytime 👌