In this article we will discuss Form Submit (Post) Example in ASP.Net Core MVC.
Here we discuss how to create Form Fields and then send data from View to Controller using Form Collection in ASP.Net Core MVC..
Before start this article, please visit our previous article Difference between IHostingEnvironment and IWebHostEnvironment
Controller
The Action method for POST operation accepts the values of First Name, Last Name and Address sent from the View and it is assigned to a ViewBag object.
View
The View consists of an HTML Form which has been created using the Razor Tag attributes with the following attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There are three TextBox fields created for capturing values for First Name, Last Name and Address. There’s also a Submit Button at the end of the Form and when the Button is clicked, the Form is submitted.
Finally, the values of the ViewBag object is displayed using Razor syntax.
Output
Let's run the application and see the result like below. You can see that when we type text on input boxes form submit/post the data into controller and it return the result.
</> Find the Source Code in Github
Summary
You May Also Like...
- Send email with attachment in .NET Core
- Difference between AddSingleton vs AddScoped vs AddTransient in asp.net core
- Dependency Injection in Asp.Net Core
- How to read values from appsettings.json in Asp.Net Core
- How to Get Application Base Path in Asp.Net Core
- Import and Export Excel file in ASP .NET Core 3.1 razor page
- Consume Restful API to get the bank details using IFSC Code in .NET Core
- Create ASP .NET Core MVC application using VS Code
- Generate QR Code in .NET Core using Bitmap
- How to Securely Open PDF in Browser using .NET Core
Post a Comment