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

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);
- }
- }
- }

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

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
- });
- }
- }
- }

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.

Pravas RanjanPosted Apr 24, 2017, 6:01 AM
Super Article Bhai...Keep going...Many Thanks....
Bikesh SrivastavaPosted Jan 25, 2017, 8:35 AM
Very nice article ,well explained.