PRACTICAL 4: STRONGLY TYPED VIEW

STEPS:
1. Create new project > select ASP.NET Web Application > select MVC > project created.
2. Models > Add > New Item > Class > new model class file (Student.cs) created > write code.
3. Go in Controllers > Add > Controller > "MVC Controller - Empty" > StudentController > write code.
4. For creating a strongly typed view: in Views create a new folder "Student".
5. In the folder Student create two views: "Index" and "Create" > write code.
6. Run the project and open /Student.

CODE: (Models/Student.cs)
namespace Prac4.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public int Age { get; set; }
        public string Course { get; set; }
    }
}

CODE: (Controllers/StudentController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Prac4.Models;

namespace Prac4.Controllers
{
    public class StudentController : Controller
    {
        private static List<Student> students = new List<Student>
        {
            new Student{Id=1,FullName="Alice Johnson",Age=21,Course="Computer Science"},
            new Student{Id=2,FullName="Bob Smith",Age=20,Course="Electrical Engineering"}
        };

        public IActionResult Index()
        {
            return View(students);
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                student.Id = students.Count + 1;
                students.Add(student);
                return RedirectToAction("Index");
            }
            return View(student);
        }
    }
}

CODE: (Views/Student/Index.cshtml)
@model IEnumerable<Prac4.Models.Student>

<h2>Student List</h2>
<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Course</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var student in Model)
        {
            <tr>
                <td>@student.FullName</td>
                <td>@student.Age</td>
                <td>@student.Course</td>
            </tr>
        }
    </tbody>
</table>

CODE: (Views/Student/Create.cshtml)
@model Prac4.Models.Student
<h2>Add New Student</h2>
<form asp-action="Create" method="post">
    <div>
        <label asp-for="FullName"></label>
        <input asp-for="FullName"/>
    </div>
    <div>
        <label asp-for="Age"></label>
        <input asp-for="Age" type="number" />
    </div>
    <div>
        <label asp-for="Course"></label>
        <input asp-for="Course" />
    </div>
    <button type="submit">Save</button>
</form>

OUTPUT:
/Student shows the Student List table; the Create page adds a new student to the list.


PRODUCT EXAMPLE (same pattern: Product model + ProductController + Index/Create views)

Model fields used: Id, Name, Category, Price  (Models/Product.cs)

CODE: (Controllers/ProductController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Prac4.Models;

namespace Prac4.Controllers
{
    public class ProductController : Controller
    {
        private static List<Product> products = new List<Product>
        {
            new Product{Id=1,Name="Notebook",Category="Stationery",Price=70},
            new Product{Id=2,Name="Mouse",Category="Electronic",Price=200},
            new Product{Id=3,Name="Jeans",Category="Clothes",Price=1000}
        };

        public IActionResult Index()
        {
            return View(products);
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                product.Id = products.Count + 1;
                products.Add(product);
                return RedirectToAction("Index");
            }
            return View(product);
        }
    }
}

OUTPUT:
Product List table with columns ID, Name, Category, Price
(1 Notebook Stationery 70 / 2 Mouse Electronic 200 / 3 Jeans Clothes 1000).
