在 ASP.NET MVC 中,ViewData 可以用于在 Controller 和 View 之间传递数据。然而,在前端页面中使用 ViewData 时,可能会遇到数据无法传递的问题。解决方法是使用 ViewBag 代替 ViewData。下面是代码示例:
Controller:
public ActionResult Index()
{
ViewBag.Message = "Hello, World!";
return View();
}
View:
@{
ViewBag.Title = "Home Page";
}
@ViewBag.Message