Multiple Form Tag Sample in MVC

Multiple Form Tag Sample in MVC

Introduction

In this article we create four forms for addition, subtraction, multiplication and division in the same page.

Procedure

Create an MVC project from the "Empty" template.

Right-click on "Controllers" and select "Add" >> "Controller...".

Select "MVC 5 Controller - Empty" to add an empty controller.

Click on the "Add" button.

Name the controller "CalcController".

The Index() action result method will be added.

To add a view, right-click on "Index" and select "Add View...".

Name the view and select "Empty (without model)" as the template. Click on the "Add" button.

Create the following four form tags.

Addition

6.jpg

Subtraction

subtraction.jpg

Multiplication

Multiplication.jpg

Division

Division.jpg

Create four action methods for the four operations.

Addition: In this method we get data using the Request.Form collection.

7.jpg

Subtraction: In this method we get data using the Request.Form.Get method.

Subtraction2.jpg

Multiplication: In this method we get data using the Request.Params indexer.

Multiplication2.jpg

Division: In this method we get data using the Request object.

Division2.jpg

We can get posted data using any of the preceding ways for all the operations but we use multiple ways for learning purposes.

Run the project, insert a value for no1 and no2 and click on the "Submit" button. The form will be submitted and we will get the result of the two values.

8.jpg