how to create a record in asp.net mvc based on something passed to the controller form a view.
how to create a record in asp.net mvc based on something passed to the controller form a view.
24 Replies
What record?
A database record?
Yup
Like I what to pass a id from a view and then in a controller basically fill up a record with the id that got passed in and also adjust the other feilds
Well that's easy, and any random tutorial should've covered it
You need to define the model of data sent to the controller, say
Then take it as a parameter in the controller action, with a proper binding method
Use that data to create the database model
And save it in the db
The
_context
being injected into the controller from DI
Hi thanks
Still struggling
Is this in the controller
I understood thsi
I donβt get what this means
Which one is parameters from the form
What dose sealed mean
Can I use @htmlaction
The record describes the parameters of the form
sealed
means it cannot be inherited from. You don't have to use it if you don't want toCan you please tell me why this way doesnβt work and why it is not returning to index page or even adding to the database.
@ZZZZZZZZZZZZZZZZZZZZZZZZZ
What's that
Index
property? Does it contain the name of the view?What
The index is default
Orderedfood controller index
Should this work why dose it Jsut say peg doesnβt work when run
This
But even if it canβt load the index page it should sitll add to database
You can pass a few things to the
View()
method
Usually, just the data you want to send to the view
And the view itself is picked by convention
Alternatively, you can pass a string with the name of the view
And data, if need be
So, what is that Index
that you're passing to View()
?Like the data is already there in the index
You can't pass a method as a value to the
Index
method
I'd suggest going through some "ASP.NET Core MVC Basics" kind of a tutorial tbhNo thatβs my index
That is your
Index
method, yesSo how do I return to it
And, as I already told you, that is NOT what you can pass to the
View()
method
RedirectToAction()
for exampleOk I see
No, not like that either
Read what the error says
Read the documentation
Go through a tutorial
Method group to string
Oh
Can you suggest any
Ok thank so much
Last question
Is the other code to add a new record fine
And if I fix the return index thing will it work
Or is that all worng as well
It seems fine, yes
goot it to work thank you