Get Integer Input and Add Two Numbers in ASP.NET Core

In this chapter you will learn

  • How to get integer input in ASP.Net Core?
  • How to add them in controller and return to View?

This is another part of Controller’s Example. In this chapter you will learn how to receive integer input from the user and returns sum of the numbers. However, it is very simple task but the motive is to make you more comfortable with controllers. Each practice you will complete will make you more experienced. So, let’s start the chapter.

1. Create New ASP.NET Core Project SumNumber. Open Visual Studio 2015 File New Project. Select Web in Left Pane and then Select ASP.NET Core Web Application (.NET Core) in central pane. Give the project name SumNumber and click OK.


2. Select Web Application in template window and set Authentication to No Authentication and click OK.


If you are getting trouble in creating new project then you can see the step by step guideline to create new project at here.

Your First ASP.NET CORE MVC Project


3. Add a View Page in Home Folder. Go to Solution Explorer and Right Click on Home Folder (It is Under Views). Click Add New Item. Select MVC View Page Template in ASP.NET Section and rename it sum.cshtml and then click to Add button.




4. Open sum.cshtml and add the following code.





Here, I created a simple form that contains two text boxes and an Add Button. I have kept textboxes names txtFirst and txtSecond. In the controller page I will access these textboxes using this name.
<form asp-controller="Home" asp-action="add" method="post">
This form indicates that all the submission will go to HomeController and add action method will be executed.

5. Now open HomeController.cs and add the following code.




In this program I have added two IAction Methods sum() and add(). Sum() method simply return the sum view page and add() method receives input from browser, process it, keep results in ViewBag.SumResult and return to browser.

Now, it’s time to run your program. Simply press Ctrl + F5 to run your program. It will launch ASP.NET Core website into browser. Simply add /Home/sum at the end of link and press enter.


In this chapter, you learned how to get integer input and process it in ASP.NET Core Website. In the next chapter, you will learn How to implement multiple submit button in a single Form in ASP.NET Core Website.

Leave a Reply

Your email address will not be published. Required fields are marked *