View to Controller Method 10: Day 32

Introduction

In this article we post data from a view to a controller using a form and get the data using a Request.

To post data from a view to a controller, we saw the following methods:

  1. Using Html.BeginForm() View To Controller Method 1.

  2. Using Html.BeginForm() by passing an action name and controller name explicitly View To Controller Method 2.

  3. Using Html.BeginForm() by passing a Form Method View To Controller Method 3.

  4. Using Html.BeginForm() by calling an action method by action name View To Controller Method 4.

  5. Using Html.BeginForm() and request object View To Controller Method 5.

  6. Using a Html form element and request object View To Controller Method 6.

  7. Using a Html form element and value provider View To Controller Method 7.

  8. Using a Html form element and Request.Form.Get.

  9. Using a Html form element and Request.Params.

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 an HTML form element and define input controls inside the form element and set post method.

    html code

  7. In the controller we get data using the Request object that is used to retrieve posted data. This object is also used to read data from the following sources:

    • Form
    • Cookies
    • Server Variables
    • Certificate Data
    • Browser information

    cs code

  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 sum of the two values.

    output
    << View to Controller Method 9: Day 31


Similar Articles