DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot

Trending

  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  • Introduction to Retrieval Augmented Generation (RAG)
  • The Perfection Trap: Rethinking Parkinson's Law for Modern Engineering Teams
  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  1. DZone
  2. Coding
  3. Frameworks
  4. Basic Web Calculator in ASP.NET MVC 5

Basic Web Calculator in ASP.NET MVC 5

In this post, we'll learn how to perform an arithmetic operation (Addition, Subtraction, Multiplication, Division) using multiple submit buttons in MVC.

By 
Satyaprakash Samantaray user avatar
Satyaprakash Samantaray
·
May. 28, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
45.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

Introduction

The MVC architectural pattern separates the user interface (UI) of an application into three main parts.

  1. The Model − A set of classes that describes the data you are working with as well as the business logic.
  2. The View − Defines how the application’s UI will be displayed. It is pure HTML, which decides how the UI is going to look.
  3. The Controller − A set of classes that handles communication from the user, overall application flow, and application-specific logic. 

MVC Description

Thi is responsible for rendering the user interface, whether that be HTML or whether it actually be UI widgets on a desktop application. The view talks to a model, and that model contains all of the data that the view needs to display. Views generally don't have much logic inside of them at all.

The controller that organizes is everything. When an HTTP request arrives for an MVC application, that request gets routed to a controller, and then it's up to the controller to talk to either the database, the file system, or the model.

Merits Of MVC

  • Provides better support for test-driven development (TDD).

  • Works well for web applications that are supported by large teams of developers and for web designers who need a high degree of control over the application behavior.

  • Direct control over HTML also means better accessibility for implementing compliance with evolving Web standards.

  • Makes it easier to manage complexity by dividing an application into the model, the view, and the controller.

Project Description

In this post, I will show how to perform an arithmetic operation (Addition, Subtraction, Multiplication, Division) using multiple submit buttons in MVC.

Steps to Be Followed

Step 1: Create an MVC application named "Operation." I named mine "Addition."

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

Step 3: Now, go to the "Models" folder and create a class file. Here, we define some entities for accessing the values through it and showing the output.

Step 4: Code Ref. for AdditionViewModel.cs

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Web;    
namespace Addition.Models {    
    public class AdditionViewModel {    
        //public int A { get; set; }    
        //public int B { get; set; }    
        //public int Result { get; set; }    
        public double A {    
            get;    
            set;    
        }    
        public double B {    
            get;    
            set;    
        }    
        public double Result {    
            get;    
            set;    
        }    
    }    
}   

Code Description

Here I used three entities to access input values using A and B given by the user and show the arithmetic operation result using the Result variable.

public double A {      
            get;      
            set;      
        }      
        public double B {      
            get;      
            set;      
        }      
        public double Result {      
            get;      
            set;      
        } 

Step5: Then, go to "Controllers" folder and create a Controller class file.

Step6: Code Ref. For HomeController.cs

using Addition.Models;    
using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Web;    
using System.Web.Mvc;    
namespace Addition.Controllers {    
    public class HomeController: Controller {    
        //    
        // GET: /Home/    
        [HttpGet]    
        public ActionResult Index() {    
                return View();    
            }    
            [HttpPost]    
        public ActionResult Index(AdditionViewModel model, string command) {    
            if (command == "add") {    
                model.Result = model.A + model.B;    
            }    
            if (command == "sub") {    
                model.Result = model.A - model.B;    
            }    
            if (command == "mul") {    
                model.Result = model.A * model.B;    
            }    
            if (command == "div") {    
                model.Result = model.A / model.B;    
            }    
            return View(model);    
        }    
    }    
}   

Code Description

In the ActionResult method, I passed one string parameter named command, and, using this command, I assigned some arithmetic operation string valuse and based on that the operation will be performed.

    public ActionResult Index(AdditionViewModel model, string command) {      
               if (command == "add") {      
                   model.Result = model.A + model.B;      
               }      
               if (command == "sub") {      
                   model.Result = model.A - model.B;      
               }      
               if (command == "mul") {      
                   model.Result = model.A * model.B;      
               }      
               if (command == "div") {      
                   model.Result = model.A / model.B;      
               }      
               return View(model);   

Step7: Go to the "Views" folder and create a folder called "Home" inside of which we'll create a cshtml file named Index.cshtml.

Step8: Code Ref. for Index.cshtml

@model Addition.Models.AdditionViewModel    
@ {    
    Layout = null;    
}    
@using(Html.BeginForm()) { < fieldset > < legend style = "color:blue" > Operation Of Two Numbers < /legend>    
    @Html.EditorFor(x => x.A) < br / > < br / > @Html.EditorFor(x => x.B) < br / > < br / > < input type = "submit"    
    value = "add"    
    name = "command" / > < input type = "submit"    
    value = "sub"    
    name = "command" / > < input type = "submit"    
    value = "mul"    
    name = "command" / > < input type = "submit"    
    value = "div"    
    name = "command" / > < br / > < br / > < h2 style = "color:red" > @Html.Label("Result is :")    
    @Html.DisplayFor(x => x.Result) < /h2> < /fieldset>    
}  

Code Description

To access input values I need textboxes.

@Html.EditorFor(x => x.A) < br / > < br / > @Html.EditorFor(x => x.B)  

Here I used a reference to the controller class that is command and related string values like add, mul, sub, and div. The button type is submit.

By using multiple submit button type, we can perform arithmetic operations.

<input type = "submit"      
    value = "add"      
    name = "command" / > < input type = "submit"      
    value = "sub"      
    name = "command" / > < input type = "submit"      
    value = "mul"      
    name = "command" / > < input type = "submit"      
    value = "div"      
    name = "command" / > < br / > < br / > < h2 style = "color:red" > @Html.Label("Result is :")      
    @Html.DisplayFor(x => x.Result)  

Finally, for showing the resulting output.

@Html.DisplayFor(x => x.Result)  

Step9: Then, go to the "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

    using System;    
    using System.Collections.Generic;    
    using System.Linq;    
    using System.Web;    
    using System.Web.Mvc;    
    using System.Web.Routing;    
    namespace Addition {    
        public class RouteConfig {    
            public static void RegisterRoutes(RouteCollection routes) {    
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");    
                routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {    
                    controller = "Home", action = "Index", id = UrlParameter.Optional    
                });    
            }    
        }    
    }     

Code Description

Here the controller name is Home and the controller action method name is Index. So here I'll put a new controller action method, and name it Index1.

Then the start page should come with ..../Home/Index1 . So, now, the URL must be ..../Home/Index.new

public static void RegisterRoutes(RouteCollection routes) {      
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");      
            routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {      
                controller = "Home", action = "Index", id = UrlParameter.Optional      
            }); 

Output

When the page loads the URL should be like this.

See the URL - http://localhost:56637/Home/Index. Here, "Home" is the Controller name and "Index" is the controller action method name.

For Addition

Image title

For Subtraction

Image title

For Multiplication

Image title

For Division

Image title

Summary

  1. The relationship between Controller, Model, View.
  2. By using this application your can perform + , - , * , /  on two numerical.
ASP.NET MVC ASP.NET application Calculator (Mac OS)

Opinions expressed by DZone contributors are their own.

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  • DGS GraphQL and Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!