ViewBag là 1 đối tượng trong ASP.NET MVC dùng để truyền dữ liệu từ Controller sang View
Trong HomeController.cs, xóa những thứ có sẵn rồi tạo 1 action mới tên là EmpDetail
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Test.Controllers
{
public class HomeController : Controller
{
public ActionResult EmpDetail()
{
ViewBag.Position = new List<string>() { "Security", "Employee", "Manager" };
return View();
}
}
}
Tạo 1 view mới tên là EmpDetail
EmpDetail.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="/redirect?Id=9fLRaWQliBbz0ECLj4fqtEVVEaknSDhDxHclZjypxm4K7BtEopPujLJ6oDaqg5Cg" rel="stylesheet" type="text/css" />
<title>Thông tin nhân viên</title>
</head>
<body>
<div class="container">
<table class="table table-hover">
<tr>
<td>Position</td>
<td>
<ul>
@foreach (var pos in ViewBag.Position)
{
<li>@pos</li>
}
</ul>
</td>
</tr>
</table>
</div>
</body>
</html>