Multiple Form Tag Sample in MVC: Day 33

Introduction

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

Procedure

  1. Create an MVC project from the "Empty" template.

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

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

    Click on the "Add" button.

  3. Name the controller "CalcController".

    The Index() action result method will be added.

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

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

  6. Create the following four form tags.

    Addition

    adding code


    Subtraction

    Subtraction code


    Multiplication

    Multiplication code


    Division

    Division code

  7. Create four action methods for the four operations.

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

    adding cs code

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

    Subtraction cs code

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

    Multiplication cs code

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

    Division cs code

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

  8. 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.

    output

Up Next
    Ebook Download
    View all
    Learn
    View all