Create Addition, Subtraction, Multiplication, Division Application Using ASP.NET MVC 4.0 With Multiple Submit Buttons

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


By default, Visual Studio created some folders for you, like Controllers, Models, Views etc.


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.


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. }   
Then, go to "Controllers" folder and create a Controller class file as mentioned in the below snap.



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. }   
Go to "Views" folder and create a folder called "Home" inside which create a cshtml file named  Index.cshtml.


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


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. }   
The output of this application will be like the following.


See the URL - http://localhost:56637/Home/Index

Here, "Home" is the Controller name and "Index" is the controller action method name.

Output








Happy coding….

Have a Nice Day and Good Luck.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now