[C#/ASP.NET MVC/.NETCORE] IViewComponentHelper 인터페이스 : InvokeAsync 메소드를 사용해 뷰 컴포넌트의 특정 뷰 호출하기
■ IViewComponentHelper 인터페이스의 InvokeAsync 메소드를 사용해 뷰 컴포넌트의 특정 뷰를 호출하는 방법을 보여준다. ▶ Models/FavoriteModel.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 FavoriteModel { //////////////////////////////////////////////////////////////////////////////////////////////////// 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 URL - URL /// <summary> /// URL /// </summary> public string URL { get; set; } #endregion } } |
▶ ViewComponents/FavoriteViewComponent.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 Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using TestProject.Models; namespace TestProject.ViewComponents { /// <summary> /// 즐겨찾기 뷰 컴포넌트 /// </summary> public class FavoriteViewComponent : ViewComponent { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 호출하기 - Invoke() /// <summary> /// 호출하기 /// </summary> /// <returns>뷰 컴포넌트 결과</returns> public IViewComponentResult Invoke(string page) { List<FavoriteModel> list = new List<FavoriteModel>() { new FavoriteModel { ID = 1, Title = "ICODEBROKER", URL = "https://icodebroker.tistory.com/" }, new FavoriteModel { ID = 2, Title = "구글" , URL = "https://www.google.com/" }, new FavoriteModel { ID = 3, Title = "유튜브" , URL = "https://www.youtube.com/" }, new FavoriteModel { ID = 4, Title = "다음" , URL = "https://www.daum.net/" }, new FavoriteModel { ID = 5, Title = "네이버" , URL = "https://www.naver.com/" } }; return View(page, list); } #endregion } } |
▶ Views/Shared/Components/Favorite/Default.cshtml
1 2 3 4 5 6 7 8 9 10 11 |
@using TestProject.Models @model List<FavoriteModel> <p>디폴트 뷰</p> <ul> @foreach(FavoriteModel favorite in Model) { <li><a href="@favorite.URL" target="_blank">@favorite.Title</a></li> } </ul> |