ASP.net MVC 4.0: Strongly-typed views to show data with calculation using Razor view



1. Controller here

employeeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvctest.Models;

namespace mvctest.Controllers
{
    public class employeeController : Controller
    {
        //
        // GET: /employee/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult inputdata()
        {
            return View();
        }
        public ActionResult showdata(employee e)
        {
            return View(e);
        }

    }
}


2. Model here

employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace mvctest.Models
{
    public class employee
    {
        public int ecode { get; set; }
        public string ename { get; set; }
        public string edept { get; set; }
        public int esal { get; set; }

    }
}

3. Views here

inputdata.cshtml

@model mvctest.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>inputdata</title>
</head>
<body>
    <div>
        <fieldset style="width:400px; margin:auto">

            <legend>Enter  Employee Data Please</legend>

            @using (Ajax.BeginForm("showdata", "employee", new AjaxOptions { UpdateTargetId = "divresult" }))
            {
                <table>
                    <tr>
                        <td>
                            @Html.Label("Enter  Code")
                        </td>
                        <td>
                            @Html.TextBox("ecode",Model)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.Label("Enter Name")
                        </td>
                        <td>
                            @Html.TextBox("ename", Model)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.Label("Enter Department")
                        </td>
                        <td>
                            @Html.TextBox("edept", Model)
                        </td>
                    </tr>
                    <tr>
                        <td>
                            @Html.Label("Enter Salary")
                        </td>
                        <td>
                            @Html.TextBox("esal",Model)
                        </td>
                    </tr>
                    <tr><td><button>submit</button></td></tr>
                </table>
                
            }
          
        </fieldset>
    </div>
</body>
</html>

showdata.cshtml

@model mvctest.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>showdata</title>
</head>
<body>
    <div>
        <fieldset style="width:400px; margin:auto">
            <legend>Here is your submitted data</legend>
            <table>
                <tr>
                    <td>
                        @Html.Label("Enter  Code")
                    </td>
                    <td>
                        @Model.ecode
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Enter Name")
                    </td>
                    <td>
                        @Model.ename
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Enter Department")
                    </td>
                    <td>
                        @Model.edept
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Enter Salary")
                    </td>
                    <td>
                        @Model.esal
                    </td>
                </tr>               
            </table>
            @{
                double x = Model.esal;
                double hra = x * .10;
                double da = x * .40;
                double cca = x * .05;
                double gross = x + hra + da + cca;
            }
            <fieldset>
                <legend>Salary Detail</legend>
                <table>
                    <tr>
                        <td>Name:</td>
                        <td>@Model.ename</td>

                    </tr>
                    <tr>
                        <td>Department:</td>
                        <td>@Model.edept</td>

                    </tr>
                    <tr>
                        <td>Salary:</td>
                        <td>@Model.esal</td>


                    </tr>
                    <tr>
                        <td>HRA:</td>
                        <td>@hra</td>

                    </tr>
                    <tr>
                        <td>DA:</td>
                        <td>@da</td>

                    </tr>
                    <tr>
                        <td>GROSS TOTAL:</td>
                        <td>@gross</td>

                    </tr>
                </table>
            </fieldset>
        </fieldset>
    </div>
</body>
</html>



Output

strr1 strr2