Back To Home

Name: Ngày 15: Tạo Controller Rendering đầu tiên

🎯 Mục tiêu Ngày 15

Sau bài này, bạn sẽ biết:

  1. Controller Rendering là gì

  2. Cách tạo View + Controller + Model trong Sitecore MVC

  3. Cách đăng ký Rendering trong Sitecore

  4. Cách gắn Rendering lên page

  5. Cách debug & best practices


🧩 1. Controller Rendering là gì?

Trong Sitecore có 3 loại rendering chính:

Rendering Mục đích
View Rendering Hiển thị HTML đơn giản, không cần controller
Controller Rendering Xử lý logic backend → trả về model → render view
JSON Rendering Dùng cho Headless

Controller Rendering = “ASP.NET MVC bình thường” nhưng chạy trong Sitecore.


🧱 2. Tạo Controller Rendering đầu tiên

Giả sử bạn muốn tạo component Navigation Menu.


📌 Bước 1 — Tạo Model

/src/Feature/Navigation/Models/MenuViewModel.cs

 
namespace Feature.Navigation.Models { public class MenuViewModel { public string Title { get; set; } public IEnumerable<string> Items { get; set; } } }

📌 Bước 2 — Tạo Controller

/src/Feature/Navigation/Controllers/MenuController.cs

 
using System.Web.Mvc; using Feature.Navigation.Models; namespace Feature.Navigation.Controllers { public class MenuController : Controller { public ActionResult MainMenu() { var model = new MenuViewModel { Title = "Main Menu", Items = new[] { "Home", "About", "Contact" } }; return View("/Views/Feature/Navigation/MainMenu.cshtml", model); } } }

Lưu ý quan trọng về Sitecore
Phải trả về View bằng path tuyệt đối.


📌 Bước 3 — Tạo View

/Views/Feature/Navigation/MainMenu.cshtml

 
@model Feature.Navigation.Models.MenuViewModel <nav> <h3>@Model.Title</h3> <ul> @foreach (var item in Model.Items) { <li>@item</li> } </ul> </nav>

🧩 3. Tạo Controller Rendering trong Sitecore

Vào Sitecore Content Editor:

 
/sitecore/layout/Renderings/Feature/Navigation/Main Menu

Ở panel bên phải:

Field Giá trị
Controller Menu
Controller Action MainMenu
Datasource Template (optional) Không cần hoặc chọn Template phù hợp
Datasource Location /sitecore/content/Data/...

Nếu bạn dùng Helix → đặt đúng folder Feature/Navigation.


🧩 4. Gán Rendering lên page

  1. Mở Experience Editor

  2. Chọn placeholder (ví dụ: navigation)

  3. Add Rendering → chọn Main Menu

  4. Publish → xem kết quả


🧪 5. Kiểm tra hoạt động

Khi load trang, Sitecore sẽ:

  1. Gọi MenuController.MainMenu()

  2. Tạo MenuViewModel

  3. Render view tại /Views/Feature/Navigation/MainMenu.cshtml

  4. Xuất HTML ra trang

Nếu bạn thấy menu → 🎉 Bạn đã tạo được Controller Rendering đầu tiên.


🛠 6. Best Practices (rất quan trọng)

✔ Controller phải đặt trong Feature hoặc Project layer

✔ View đặt đúng đường dẫn Helix

✔ Luôn dùng absolute path khi return view

✔ Không viết logic trong View

✔ Không hardcode path trong Controller (chỉ View path là ok)

✔ Nếu component có datasource → xử lý null datasource

Ví dụ xử lý datasource null:

 
var item = Sitecore.Context.Item; if (RenderingContext.Current.Rendering.Item != null) { item = RenderingContext.Current.Rendering.Item; }

Donald Trump

Để trở thành người chiến thắng, bạn cần biết khi nào là đủ. Đôi khi trong cuộc sống, bạn phải từ bỏ cuộc chiến và chuyển sang mục tiêu mới mang lại hiệu quả hơn

Related Post