Our Application name is Calculator.
1)Right click on Controller and Add>Controller
2)First of all we are make one controller that is name AdditionController.cs and write following code.
(AdditionController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Calculator.Models;
namespace Calculator.Controllers
{
public class AdditionController : Controller
{
//
// GET: /Addition/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(DataModel m)
{
m.Result1 = m.FNum + m.SNum;
m.Result2 = m.FNum - m.SNum;
try
{
m.Result3 = m.FNum * m.SNum;
m.Result4 = m.FNum / m.SNum;
}
catch
{
}
return View(m);
}
}
}
3)Right click on ActionResult which is located into AdditionController.cs file
4)Add View>Add You can show one file which is .cshtml file.
5)write following code into that file.
(Index.cshtml)
@model Calculator.Models.DataModel
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<table border="2">
<tr>
<td>@Html.LabelFor(m => m.FNum) </td>
<td>@Html.TextBoxFor(m=>m.FNum)</td>
</tr>
<tr>
<td>
@Html.LabelFor(m => m.SNum)
</td>
<td>
@Html.TextBoxFor(m=>m.SNum)
</td>
</tr>
<tr>
@if(@Model!=null)
{
<td>
@Html.LabelFor(m=>m.Result1)
@Model.Result1
@Html.LabelFor(m=>m.Result2)
@Model.Result2
@Html.LabelFor(m=>m.Result3)
@Model.Result3
@Html.LabelFor(m=>m.Result4)
@Model.Result4
</td>
}
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Calculate"/>
</td>
</tr>
</table>
}
</div>
</body>
</html>
6)Right click on Model folder>Add>Class
7)You can give name of your DataModel.cs
8)Write following code in your Model file.
(DataModel.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using Calculator.Models;
namespace Calculator.Models
{
public class DataModel
{
[DisplayName("First Number")]
public int FNum { get; set; }
[DisplayName("Second Number")]
public int SNum { get; set; }
[DisplayName("Addition of two number")]
public int Result1 { get; set; }
[DisplayName("Subtraction of two number")]
public int Result2 { get; set; }
[DisplayName("Multiplication of two number")]
public int Result3 { get; set; }
[DisplayName("Division of two number")]
public int Result4 { get; set; }
}
}
1)Right click on Controller and Add>Controller
2)First of all we are make one controller that is name AdditionController.cs and write following code.
(AdditionController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Calculator.Models;
namespace Calculator.Controllers
{
public class AdditionController : Controller
{
//
// GET: /Addition/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(DataModel m)
{
m.Result1 = m.FNum + m.SNum;
m.Result2 = m.FNum - m.SNum;
try
{
m.Result3 = m.FNum * m.SNum;
m.Result4 = m.FNum / m.SNum;
}
catch
{
}
return View(m);
}
}
}
3)Right click on ActionResult which is located into AdditionController.cs file
4)Add View>Add You can show one file which is .cshtml file.
5)write following code into that file.
(Index.cshtml)
@model Calculator.Models.DataModel
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<table border="2">
<tr>
<td>@Html.LabelFor(m => m.FNum) </td>
<td>@Html.TextBoxFor(m=>m.FNum)</td>
</tr>
<tr>
<td>
@Html.LabelFor(m => m.SNum)
</td>
<td>
@Html.TextBoxFor(m=>m.SNum)
</td>
</tr>
<tr>
@if(@Model!=null)
{
<td>
@Html.LabelFor(m=>m.Result1)
@Model.Result1
@Html.LabelFor(m=>m.Result2)
@Model.Result2
@Html.LabelFor(m=>m.Result3)
@Model.Result3
@Html.LabelFor(m=>m.Result4)
@Model.Result4
</td>
}
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Calculate"/>
</td>
</tr>
</table>
}
</div>
</body>
</html>
6)Right click on Model folder>Add>Class
7)You can give name of your DataModel.cs
8)Write following code in your Model file.
(DataModel.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using Calculator.Models;
namespace Calculator.Models
{
public class DataModel
{
[DisplayName("First Number")]
public int FNum { get; set; }
[DisplayName("Second Number")]
public int SNum { get; set; }
[DisplayName("Addition of two number")]
public int Result1 { get; set; }
[DisplayName("Subtraction of two number")]
public int Result2 { get; set; }
[DisplayName("Multiplication of two number")]
public int Result3 { get; set; }
[DisplayName("Division of two number")]
public int Result4 { get; set; }
}
}