Add two numbers
Add two numbers
Let us add 2 numbers using mvc architecture. We could do this in a separate controller or in the existing home controller like shown below
Controller Code
public class HomeController : Controller
{
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(int firstNum, int secondNum)
{
int result = firstNum + secondNum;
ViewBag.FirstNum = firstNum;
ViewBag.SecondNum = secondNum;
ViewBag.Result = result;
return View();
}
}View Code
In ASP.NET Core MVC, there are three ways to pass data from a controller to a view:
using
ViewData,using
ViewBag, andusing strongly-typed models.
Last updated