Saturday, 8 May 2021

how to sum two number in mvc

 Overview:


This article explains how to create a  sum of two numbers using model, controller, view in ASP.NET MVC. then pass the data from the view to the controller using by model. The following steps here.

DownloadCode.zip

Step-1:

Create a new project in visual studio 2019.


sum two numbers mvc






Step-2:

Create a Model Class in Model folder - Right click on Model folder - Add new class - enter the class name as SumModels.cs



create model mvc

Step-3:

if you don't have a controller, Create a new controller in the controller folder. - Right-click on Model folder- Add new controller class enter the name as HomeController.cs

Or  HomeController.cs is created automatically when you create a project.

sum two numbers mvc




Step-4:

Create a new view page from the controller- Right-click on the Controller action method then create the view.

add two numbers mvc




Model Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;


namespace AddTwoNumberMVC.Models

{

    public class SumModels

    {

        public int A { get; set; }

        public int B { get; set; }


        public int Total { get; set; }

    }

}


Controller code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using AddTwoNumberMVC.Models;



namespace AddTwoNumberMVC.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }


        public ActionResult About()

        {

            ViewBag.Message = "Your application description page.";


            return View();

        }


        public ActionResult Contact()

        {

            ViewBag.Message = "Your contact page.";


            return View();

        }


        [HttpGet]

        public ActionResult Sum()

        {

            return View();

        }


        [HttpPost]

        public ActionResult Sum(SumModels sum)

        {

            sum.Total = sum.A + sum.B;


            return View(sum);

        }

    }

}


View Code:

@model AddTwoNumberMVC.Models.SumModels


@{

    ViewBag.Title = "Sum";

}


<h2>Sum</h2>


<div class="row">

    @using (Html.BeginForm("Sum", "Home"))

    {

        <div class="col-lg-3">A value :@Html.TextBoxFor(Model => Model.A, new { @class = "form-control" })</div>

        <div class="col-lg-3">B value:@Html.TextBoxFor(Model => Model.B, new { @class = "form-control" })</div>

        <br />

        <div class="col-lg-3">Total:<wbr /><wbr /> @Html.DisplayFor(Model => Model.Total, new { @class = "form-control" })</div>

        <div class="col-lg-3"><button type="submit" value="Sum" class="btn btn-primary">Sum</button></div>

    }

</div>


Final Output:

sum two numbers in mvc




More Details:

https://www.youtube.com/watch?v=6R__xst_PMk


Summary

 This tutorial explains, how to create a sum of two numbers in asp.net MVC C#. Learn more and share with your friends and relatives. any comments are given below.

Thank you for reading my blogs..!!

No comments:

Post a Comment

ASP.NET MVC Full Course | In Tamil

  Overview: In this article, we will learn ASP.NET MVC Full Course in Tamil. We can learn additionally C#, SQL,  Bootstrap ..etc.  Model Pop...