ASP.net MVC 4.0: Strongly-typed views to show data with “if condition & for loop” using Razor view

1. Add Model

employee.cs

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

namespace Mangolia.Models
{
    public class employee
    {
        public int ecode { get; set; }
        public String ename { get; set; }
        public String edept { get; set; }
        public Double  esal { get; set; }

    }
}

2. Add Controller

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mangolia.Models;
namespace Mangolia.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult ShowData( employee  e )
        {
            return View(e);
        }

        public ActionResult InputData()
        {
            return View();
        }
    }
}

3. Add Views

InputData.cshtml

@model Mangolia.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>InputData</title>
</head>
<body>
    <div>
        <fieldset style="width:300px; margin:auto">
            <legend style="font-weight:bold">Enter  Employee Data Please</legend>
            @using (Ajax.BeginForm("ShowData", "Home", 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 Mangolia.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShowData</title>
</head>
<body>
    <div>      
        @{
            
            String[] rate = { "10%", "5%", "2%"};
            String[] Gr = { ">=10000", ">=5000", "<5000" };
            
            double x = Model.esal;
           
            double hra= x * .10;
            double da = x * .40;
            double cca= x * .05;
            double gross = x + hra + da + cca;
            
        }
        <fieldset style="width:300px; margin:auto">
            <legend style="font-weight:bold">Employee 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</td>
                    <td>@gross</td>

                </tr>


                <tr>
                    <td>  Tax</td>
                    <td>
                        @{
                            double tax = 0;
                        }
                        @if (gross >= 10000)
                        {
                            tax = gross * .10;
                        }
                        else if (gross >= 5000)
                        {
                            tax = gross * .05;

                        }
                        else
                        {
                            tax = gross * .02;
                        }
                        @tax
                    </td>
                </tr>
            </table><br /><br /><fieldset style="text-align:center">
                <legend style="font-weight:bold"> Taxation rate  Year  2015-2016 </legend>
                <table align="center">
                    <tr>  <th> Amount </th> <th>Rate </th></tr>
                    @{
                        int y = 0;
                        for (y = 0; y < rate.Length; y++)
                        {
                            <tr>  <td> @Gr[y]</td> <td>@rate[y] </td></tr>

                        }
                    }
                </table>

            </fieldset>

        </fieldset>

    </div>
</body>
</html>

Output here

iinp iishow