■ ViewComponent 클래스에서 뷰 컴포넌트를 사용하는 방법을 보여준다.
▶ Models/DataModel.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 |
namespace TestProject.Models { /// <summary> /// 데이터 모델 /// </summary> public class DataModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 성명 - Name /// <summary> /// 성명 /// </summary> public string Name { get; set; } #endregion #region 직함 - Title /// <summary> /// 직함 /// </summary> public string Title { get; set; } #endregion } } |
▶ Models/DataRepository.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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System.Collections.Generic; using System.Linq; namespace TestProject.Models { /// <summary> /// 데이터 저장소 /// </summary> public class DataRepository { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 리스트 /// </summary> private readonly List<DataModel> sourceList = new List<DataModel>() { new DataModel { ID = 1, Name = "김철수", Title = "차장" }, new DataModel { ID = 2, Name = "이영희", Title = "과장" }, new DataModel { ID = 3, Name = "홍길동", Title = "대리" } }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region 리스트 구하기 - GetList() /// <summary> /// 리스트 구하기 /// </summary> /// <returns>리스트</returns> public List<DataModel> GetList() { return this.sourceList; } #endregion #region 리스트 구하기 - GetList(name) /// <summary> /// 리스트 구하기 /// </summary> /// <param name="name">성명</param> /// <returns>리스트</returns> public List<DataModel> GetList(string name) { return this.sourceList.Where(n => n.Name.ToLower().Equals(name.ToLower())).ToList(); } #endregion #region 데이터 구하기 - GetData(id) /// <summary> /// 데이터 구하기 /// </summary> /// <param name="id">ID</param> /// <returns>데이터</returns> public DataModel GetData(int id) { return this.sourceList.Where(n => n.ID == id).SingleOrDefault(); } #endregion } } |
▶ ViewComponents/DataListViewComponent.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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; using TestProject.Models; namespace TestProject.ViewComponents { /// <summary> /// 데이터 리스트 뷰 컴포넌트 /// </summary> public class DataListViewComponent : ViewComponent { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 비동기 호출하기 - InvokeAsync(name) /// <summary> /// 비동기 호출하기 /// </summary> /// <param name="name">성명</param> /// <returns>뷰 컴포넌트 결과 태스크</returns> public async Task<IViewComponentResult> InvokeAsync(string name) { List<DataModel> list = await GetListAsync(name); return View(list); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 리스트 비동기 구하기 - GetListAsync(name) /// <summary> /// 리스트 비동기 구하기 /// </summary> /// <param name="name">성명</param> /// <returns>리스트 태스크</returns> private Task<List<DataModel>> GetListAsync(string name) { return Task.FromResult(GetList(name)); } #endregion #region 리스트 구하기 - GetList(name) /// <summary> /// 리스트 구하기 /// </summary> /// <param name="name">성명</param> /// <returns>리스트</returns> private List<DataModel> GetList(string name) { DataRepository repository = new DataRepository(); return repository.GetList(name); } #endregion } } |
▶ Views/Shared/Components/DataList/Default.cshtml
1 2 3 4 5 6 7 8 9 10 |
@using TestProject.Models @model List<DataModel> <ul> @foreach(DataModel data in Model) { <li>@data.ID, @data.Name, @data.Title</li> } </ul> |
▶ Controllers/DataController.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 |
using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using TestProject.Models; namespace TestProject.Controllers { /// <summary> /// 데이터 컨트롤러 /// </summary> public class DataController : Controller { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 인덱스 페이지 처리하기 - Index() /// <summary> /// 인덱스 페이지 처리하기 /// </summary> /// <returns>액션 결과</returns> public IActionResult Index() { DataRepository repository = new DataRepository(); List<DataModel> list = repository.GetList(); return View(list); } #endregion } } |
▶ Views/Data/Index.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@using TestProject.Models @model IEnumerable<DataModel> @{ ViewData["Title"] = "Data Page"; } <h3>데이터 목록 출력</h3> <ul> @foreach (DataModel data in Model) { <li>@data.ID : @data.Name, @data.Title</li> } </ul> <hr /> <h3>뷰 컴포넌트 사용</h3> <div> @await Component.InvokeAsync("DataList", new { name = "이영희" }) </div> |