HttpPost forms in ASP.NET
Hello, i'm having a hard time understanding how clicking the submit button on this form triggers the CreateCategory(Category entry) IActionResult method.
I understand that [HttpPost] determines which overload of the CreateCategory() method gets activated.. But why CreateCategory()?? There's nothing in the form linking to this. If it's automatic then how would it make distinctions between multiple [HttpPost] methods?
21 Replies
its possible specify [HttpPost("custom/path/here")].
your CreateCategory method doesnt explicitly provide a custom path.
this means path will be path of the controller.
so with that in mind, when your view (GET CreateCategory) gets loaded, a submit button on that view with no explicit path set will point to same path as it's current page.
so you click the button and it just sends a POST to current url.
i recommend you to read this for more detail
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0
Routing to controller actions in ASP.NET Core
Learn how ASP.NET Core MVC uses Routing Middleware to match URLs of incoming requests and map them to actions.
i still dont understand how i can get my form to point towards a specific POST method
here i am specifying the path of my HttpPost method, but not the submit button on a form that points to it
i didnt say you need to change anything 😄
i know i mean, it works, but what if i have multiple HttpPost methods
then how will the program know which one to activate at the press of "submit"?
or is that something asp.net devs don't concern with.. im not sure
you submit to a different path..
idk where is the relevant docs, but explore around that sub page
you would specify probably in a html tag which path your submit button sends a POST to
as an attribute of form probably
if you know how it's done in plain HTML form then it probably makes sense
ive tried action="Category/CreateCategory" but fails
where
actually take a look here
in the form
Tag Helpers in forms in ASP.NET Core
Describes the built-in Tag Helpers used with Forms.
and the page right below it
okay ill check it out
Thank you mister cat
😄
trying dis
boo womp
oh wait
no im silly
yeah im silly, i shouldnt write "CategoryController" but "Category"
it now works perfectly
nice
>looks at documentation
>tag works fine
"guhh? How could this be?!"
pointman misadventures
haha
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered