From the course: Learning ASP.NET Core MVC

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Post form data to controller actions

Post form data to controller actions

From the course: Learning ASP.NET Core MVC

Start my 1-month free trial

Post form data to controller actions

- [Instructor] Now that we've got an HTML form that allows users to post data to our application, we'll need a controller action to actually accept that post data. Since ASP.NET Core MVC is posting back to the same controller name as the action that rendered the view, I'm going to start by copying and pasting the Create controller action that I just created and use that to handle the post action. Once I do this, I'll need to specify which of these actions should show the form and which one should handle the form post. This is actually pretty easy to do. I'll simply decorate each action with the corresponding attribute: HttpGet to mark which one is displaying the view and HttpPost to indicate the controller action that should handle the form post. There's another problem, though. Even with those attributes in place, the two method signatures are exactly the same, which means that this code isn't even valid C#, so our site won't even compile. That's also simple enough to fix. I'll just…

Contents