■ Controller 클래스의 View 메소드를 사용해 뷰에 컬렉션 데이터를 전달하는 방법을 보여준다.
▶ Models/MovieModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; namespace TestProject.Models { /// <summary> /// 영화 모델 /// </summary> public class MovieModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 제목 - Title /// <summary> /// 제목 /// </summary> public string Title { get; set; } #endregion #region 생성일 - CreateDate /// <summary> /// 생성일 /// </summary> public DateTime CreateDate { get; set; } #endregion } } |
▶ Controllers/MovieController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using TestProject.Models; namespace TestProject.Controllers { /// <summary> /// 영화 컨트롤러 /// </summary> public class MovieController : Controller { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 인덱스 페이지 처리하기 - Index() /// <summary> /// 인덱스 페이지 처리하기 /// </summary> public IActionResult Index() { List<MovieModel> list = new List<MovieModel>() { new MovieModel { ID = 1, Title = "원더 우먼" , CreateDate = new DateTime(2017, 1, 1) }, new MovieModel { ID = 2, Title = "베놈" , CreateDate = new DateTime(2018, 1, 1) }, new MovieModel { ID = 3, Title = "터미네이터 - 다크 페이트", CreateDate = new DateTime(2019, 1, 1) } }; return View(list); } #endregion } } |
▶ Views/Movie/Index.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
@model List<MovieModel> @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Controller 클래스 : View 메소드를 사용해 뷰에 컬렉션 데이터 전달하기</title> <style> table { border-collapse : collapse; } table th, table td { border : 1px solid black; padding : 10px; } </style> </head> <body> <h1>영화 목록</h1> <table> <tr> <th>번호</th> <th>제목</th> <th>개봉일</th> </tr> @foreach(MovieModel movie in Model) { <tr> <td>@movie.ID</td> <td>@movie.Title</td> <td>@movie.CreateDate.Year.ToString()</td> </tr> } </table> </body> </html> |