ASP.NET - MVC - Controller, View và Model

Tích hợp Controller, View và Model 

Chúng ta đã tạo StudentController, Model View trong các phần trước, nhưng chúng ta chưa tích hợp tất cả các thành phần này để chạy nó.

Đoạn mã sau đây của  StudentController, Model View trong lớp Student được tạo trong các phần trước

StudentController:

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

namespace MVC_BasicTutorials.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            return View();
        }
    }
}

Lớp Model Student

namespace MVC_BasicTutorials.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public int Age { get; set; }
    }
}

Tập tin Index.cshtml để hiển thị danh sách sinh viên

@model IEnumerable<MVC_BasicTutorials.Models.Student>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.StudentName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentId  }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
        </td>
    </tr>
}

Bây giờ, chúng ta cần truyền dữ liệu trong Model từ Controller sang View. Chúng ta sử dụng IEnumerable để lưu trữ các đối tượng sinh viên như sau:

public class StudentController : Controller
{
    // GET: Student
    public ActionResult Index()
    {
        var studentList = new List<Student>{ 
                            new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
                            new Student() { StudentId = 2, StudentName = "Steve",  Age = 21 } ,
                            new Student() { StudentId = 3, StudentName = "Bill",  Age = 25 } ,
                            new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
                            new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
                            new Student() { StudentId = 4, StudentName = "Chris" , Age = 17 } ,
                            new Student() { StudentId = 4, StudentName = "Rob" , Age = 19 } 
                        };
        // Get the students from the database in the real application

        return View(studentList);
    }
}

Chúng ta đã tạo danh sách các đối tượng sinh viên cho ví dụ (trong ứng dụng thực tế, bạn có thể lấy nó từ cơ sở dữ liệu). Sau đó chúng ta truyền đối tượng danh sách này làm tham số trong phương thức View (). Phương thức View () được định nghĩa trong lớp Controller, tự động liên kết đối tượng Model  với View.

Bây giờ, bạn có thể chạy project MVC bằng cách nhấn F5 cho kết quả:

create view in asp.net mvc