View to Controller Method 2: Day 17

Introduction

In this article we will see how to pass data from a view to a controller using Html.BeginForm() by explicitly passing an action method and controller.

Step 1

Create a MVC project from the "Empty" template. Right-click on "Controllers" and select "Add" >> "Controller...".

Step 2

Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.

Step 3

Name the controller "CalcController"; an Index() action result method will be added.

Step 4

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

Step 5

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

Step 6

Here we pass an action method named "Add" and controller name "Calc" in html.beginform(). So it is not necessary for the action method name to be "Index".

view in  MVC

Step 7

Create an action method with name "Add" and when return the view, pass the view name "Index" as the parameter.

create an index in MVC controller

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

access index page

show result

Similar Articles