Arithmetic Operation In ASP.NET MVC

Step1
 
Create an MVC application named "Operation". I named mine "Addition".

Step2
 
By default, Visual Studio created some folders for you, like Controllers, Models, Views etc.
 
Step3
 
Now, go to "Models" folder and create a class file. Here, we define some entities for accessing the values through it and showing the output.
 
Step4
 
Code Ref. For AdditionViewModel.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. namespace Addition.Models {    
  6.     public class AdditionViewModel {    
  7.         //public int A { get; set; }    
  8.         //public int B { get; set; }    
  9.         //public int Result { get; set; }    
  10.         public double A {    
  11.             get;    
  12.             set;    
  13.         }    
  14.         public double B {    
  15.             get;    
  16.             set;    
  17.         }    
  18.         public double Result {    
  19.             get;    
  20.             set;    
  21.         }    
  22.     }    
  23. }     
Code Description
 
Here I used three entities to access input values using A and B given by user and show arithmetic operation result using Result variable.
  1. public double A {      
  2.             get;      
  3.             set;      
  4.         }      
  5.         public double B {      
  6.             get;      
  7.             set;      
  8.         }      
  9.         public double Result {      
  10.             get;      
  11.             set;      
  12.         }      
Step5
 
Then, go to "Controllers" folder and create a Controller class file.

Step6
 
Code Ref. For HomeController.cs
  1. using Addition.Models;    
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Linq;    
  5. using System.Web;    
  6. using System.Web.Mvc;    
  7. namespace Addition.Controllers {    
  8.     public class HomeController: Controller {    
  9.         //    
  10.         // GET: /Home/    
  11.         [HttpGet]    
  12.         public ActionResult Index() {    
  13.                 return View();    
  14.             }    
  15.             [HttpPost]    
  16.         public ActionResult Index(AdditionViewModel model, string command) {    
  17.             if (command == "add") {    
  18.                 model.Result = model.A + model.B;    
  19.             }    
  20.             if (command == "sub") {    
  21.                 model.Result = model.A - model.B;    
  22.             }    
  23.             if (command == "mul") {    
  24.                 model.Result = model.A * model.B;    
  25.             }    
  26.             if (command == "div") {    
  27.                 model.Result = model.A / model.B;    
  28.             }    
  29.             return View(model);    
  30.         }    
  31.     }    
  32. }     
Code Description
 
In action result method I passed one string parameter named command and using this command I assigned some arithmetic operation string value and based on that the operation will be performed.
  1. public ActionResult Index(AdditionViewModel model, string command) {      
  2.            if (command == "add") {      
  3.                model.Result = model.A + model.B;      
  4.            }      
  5.            if (command == "sub") {      
  6.                model.Result = model.A - model.B;      
  7.            }      
  8.            if (command == "mul") {      
  9.                model.Result = model.A * model.B;      
  10.            }      
  11.            if (command == "div") {      
  12.                model.Result = model.A / model.B;      
  13.            }      
  14.            return View(model);   
Step7
 
Go to "Views" folder and create a folder called "Home" inside which creates a cshtml file named Index.cshtml.

Step8
 
Code Ref. For Index.cshtml
  1. @model Addition.Models.AdditionViewModel    
  2. @ {    
  3.     Layout = null;    
  4. }    
  5. @using(Html.BeginForm()) { < fieldset > < legend style = "color:blue" > Operation Of Two Numbers < /legend>    
  6.     @Html.EditorFor(x => x.A) < br / > < br / > @Html.EditorFor(x => x.B) < br / > < br / > < input type = "submit"    
  7.     value = "add"    
  8.     name = "command" / > < input type = "submit"    
  9.     value = "sub"    
  10.     name = "command" / > < input type = "submit"    
  11.     value = "mul"    
  12.     name = "command" / > < input type = "submit"    
  13.     value = "div"    
  14.     name = "command" / > < br / > < br / > < h2 style = "color:red" > @Html.Label("Result is :")    
  15.     @Html.DisplayFor(x => x.Result) < /h2> < /fieldset>    
  16. }     
Code Description
 
To access input values I need textboxes.
  1. @Html.EditorFor(x => x.A) < br / > < br / > @Html.EditorFor(x => x.B) 
Here I used reference of controller class that is command and related string values like add , mul , sub and div. The button type is submit.
 
Using multiple submit button type we performed arithmatic operations.
  1. <input type = "submit"      
  2.     value = "add"      
  3.     name = "command" / > < input type = "submit"      
  4.     value = "sub"      
  5.     name = "command" / > < input type = "submit"      
  6.     value = "mul"      
  7.     name = "command" / > < input type = "submit"      
  8.     value = "div"      
  9.     name = "command" / > < br / > < br / > < h2 style = "color:red" > @Html.Label("Result is :")      
  10.     @Html.DisplayFor(x => x.Result) 
Finally for showing result output.
  1. @Html.DisplayFor(x => x.Result) 
Step9
 
Then, go to "App_Start" folder and open the RouteConfig.cs file to add some code to set the start page when the application will load the first time, like ASP.NET. 
 
Code Ref. For RouteConfig.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. using System.Web.Routing;    
  7. namespace Addition {    
  8.     public class RouteConfig {    
  9.         public static void RegisterRoutes(RouteCollection routes) {    
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {    
  12.                 controller = "Home", action = "Index", id = UrlParameter.Optional    
  13.             });    
  14.         }    
  15.     }    
  16. }     
Code Description
 
Here the controller name is Home and Controller action method name is Index , if I put new controller action mehod name Index1.
Then the start page should come with ..../Home/Index1 . So , as for now the url must be ..../Home/Index.new
  1. public static void RegisterRoutes(RouteCollection routes) {      
  2.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");      
  3.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {      
  4.                 controller = "Home", action = "Index", id = UrlParameter.Optional      
  5.             });    
Summary
  1. Relationship between Controller , Model , View.
  2. By using this application your can perform + , - , * , /  on two numerical.


Similar Articles