gpt4 book ai didi

c# - 从 View (.cshtml) 访问 viewbag 值

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:04 24 4
gpt4 key购买 nike

您好有人可以帮助我如何从我的 View (.cshtml) 访问 viewbag 值这是我的 sample

var appointments = new[,] { { "4/1/2013", "B'day" }, { "4/2/2013", "Appointment with abc" } };
ViewBag.Appointments = appointments;

现在我想从我的 .cshtml 文件访问 ViewBag.Appointments 值。

有什么想法吗?

最佳答案

这就是您将如何访问 ViewBag 中的数据。请记住,ViewBag 数据通常是动态的,只能在运行时访问。

ViewBag.Appointments = appointments;

在 Controller 中设置 ViewBag 数据后,在您的 View 中检索它,如下所示。

@{
var appointments = ViewBag.Appointments;
}

然后您可以使用 foreach 循环遍历结果以获取单个项目。

@foreach(var appointment in appointments)
{
<p>@appointment.value</p>
}

希望这对您有所帮助!

关于c# - 从 View (.cshtml) 访问 viewbag 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16052425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com