ASP.net MVC 4.0: submit & show data using Model and Strongly-typed view (cshtml-Razor C#)

1.  Add  model as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace softaid.Models
{
    public class employee
    {
        public int ecode { get; set; }
        public String ename { get; set; }
        public String edept { get; set; }
        public Double salary { get; set; }
    }

}

2. Add Controller

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

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AcceptData()
        {
        return View();
        }
        [HttpPost]
        public ActionResult ShowData(  employee  e)
        {
            return View(e);
        }

    }
}

3. Add View  AcceptData( Strongly-typed)

123

 

3.1 Strongly-types view for AcceptData.cshtml

@model softaid.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AcceptData</title>
</head>
<body>
    <div>
        <fieldset>
            <legend> Enter  Employee Data Please</legend>
          
            @using (@Html.BeginForm("ShowData", "Home", FormMethod.Post))  
            {
      
            <ol>
            <li>
            @Html.Label("Enter Code")
            @Html.TextBox("ecode",Model)
            </li>
            <li>
            @Html.Label("Enter Name")
            @Html.TextBox("ename",Model)
            </li>
            <li>
            @Html.Label("Enter  Department ")
            @Html.TextBox("edept", Model)
            </li>
            <li>
             @Html.Label("Enter  Salary ")
             @Html.TextBox("salary", Model)
              </li>
        </ol>
            
            <button> Submit</button>
            }
            
        </fieldset>      
    </div>
</body>
</html>

4. Add View  ShowData( Strongly-typed)

123_2

 

4.1 Strongly-types view for ShowData.cshtml

@model softaid.Models.employee

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ShowData</title>
</head>
<body>
    <div>
        <fieldset>
            <legend> Here is your Submitted Data</legend>
            Code ::@Model.ecode<br />
            Name ::@Model.ename<br />
            Department:: @Model.edept<br />
            Job Category:: 
                        
            @if(Model.salary>10000)
            {
                <span>Class-A</span>
            }
            else if (Model.salary > 5000)
            {
                <span>Class-B</span>
            }
            else
            {
                <span>Class-C</span>
            }

            

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

5. Run the project..

5.1

123_3

5.2

123_4