Arithmetic operation in ASP.NET MVC


Introduction : This is a simple ASP.NET MVC application that performs arithmetic operations. ASP.NET MVC is the advanced version of ASP.NET. MVC is the three separated part that name is the models,views,controllers. Every part performs different work in an ASP.NET  MVC application. Models is perform the business logic, Views is provide  the graphical user interface and last controller handle all views and models request. In this example we simple perform the arithmetic operation addition, subtraction, multiplication and division. In this program first we create the business logic add a class in model folder and second is pass the request in controller folder after that create a view. This is the simple application for beginners that help how to perform the arithmetic operation using  ASP.NET MVC tools.

Step1: Open the MVC ASP.NET application.

  • Open Visual Studio 2010.
  • Click file ->New->Projects.
  • Add a MVC ASP. NET 2 Empty Web application.

start111.gif

Step2: Add business logic in Models folder.

  • Right Click the models folder and add a class.
  • Right Click->add ->add new items->add class.
  • The Class name is "manish".

openmodelclass.gif

start111.gif

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace math123.Models
{
    public class Manish
    {
        public int sum(int a, int b)
        {
            int c = a + b;
            return c;
        } 
        public
int sub(int a, int b)
        {
            int c = a + b;
            return c;
        }
        public int mul(int a, int b)
        {
            int c = a * b;
            return c;
        }
        public int div(int a, int b)
        {
            int c = a / b;
            return c;
        }
    }
}

Step 3: Add a controller.

  • Right Click the controllers folder and add a controller.
  • Right Click->add ->add controller.
  • Controller name is "manu"
  • Add namespace in controller that name is "using math123.Models".

addcontroller.gif

copntrolleer.gif

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using math123.Models;
namespace math123.Controllers
{
    public class manuController : Controller
    {
        //
        // GET: /manu/
        public ActionResult Index()
        {
            Manish man = new Manish();
            man.sum(10,20);
            man.sub(30, 10);
            man.mul(10, 50);
            man.div(100, 5);
            return View(man);
        }
    }
}

Step 4: Add a view.

  • Right Click on "ActionResult" and add view.
  • The view is the strong type.
  • The view name is the default name "index".

view.gif

viewdesign.gif

Code:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<math123.Models.Manish>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox2.Text = Model.sum(10, 20).ToString();
        TextBox3.Text = Model.sub(30, 10).ToString();
        TextBox4.Text = Model.mul(10, 50).ToString();
        TextBox5.Text = Model.div(100, 5).ToString();
    }
</script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title
>
</head>
<
body bgcolor="#008040">
    <form id="form1" runat="server">
    <div style="background-color: #66CCFF">
    Sum of Number
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Sub of Number
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        Mul of Number
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        Div of Number
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <br />
    </div>
    </form
>
</body>
</
html>

Step 5: Press crtl + f5 and show the result.

Output:

output.gif

Up Next
    Ebook Download
    View all
    Learn
    View all