C# 从视图 (.cshtml) 访问视图包值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16052425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:50:09  来源:igfitidea点击:

Access viewbag values from view (.cshtml)

c#asp.netmultidimensional-arrayviewbagkendo-asp.net-mvc

提问by jestges

Hi Can someone help me how to access viewbag values from my view (.cshtml) Here is my sample

嗨,有人可以帮助我如何从我的视图 (.cshtml) 中访问 viewbag 值,这是我的示例

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

Now I want to access ViewBag.Appointments values from my .cshtml file.

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

Any idea?

任何的想法?

回答by Piotr Stapp

To loop through this array and "print it" in cshtml use following code:

要循环遍历此数组并在 cshtml 中“打印它”,请使用以下代码:

@foreach(var appointement in ViewBag.Appointments)
{
   <span>@appointement[0]</span>
   <span>@appointement[1]</span>
}

回答by Patrick D'Souza

To print all the values of the viewbag in a view in a single column use the following

要在单个列中的视图中打印 viewbag 的所有值,请使用以下命令

@{
    for (int i = 0; i < ViewBag.Appointments.GetLength(0); i++)
    {
        for (int j = 0; j < ViewBag.Appointments.GetLength(1); j++)
        { 
           <B>  @ViewBag.Appointments[i, j] </B> <br />
        }
    }    
}

To print all the values of the viewbag in a view as key value pairs then use the following

要将视图中 viewbag 的所有值打印为键值对,请使用以下命令

@{
    for (int i = 0; i < ViewBag.Appointments.GetLength(0); i++)
    {
           <B>  Key= @ViewBag.Appointments[i, 0], Value= @ViewBag.Appointments[i, 1] </B> <br />
    }    
}

EDIT- As requested in Comment below

编辑- 根据下面评论中的要求

You can have a look at this NGONproject on GIT Hub. Note: on the same page, you will find a detailed tutorial.

你可以在 GIT Hub 上查看这个NNGO项目。注意:在同一页面上,您会找到详细的教程。

A small extract from the help page to get you started.

帮助页面的一小段摘录,可帮助您入门。

In your controller, you can add any value to the dynamic NGon property of the ViewBag:

在您的控制器中,您可以向 ViewBag 的动态 NGon 属性添加任何值:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.NGon.Appointments= new[,] { { "4/1/2013", "B'day" }, { "4/2/2013", "Appointment with abc" } };
        return View();
    }
}

Then in you script function, you can use it as shown below

然后在你的脚本函数中,你可以使用它,如下所示

<script type="text/javascript">
    $(function () {

          var appointments= ngon.Appointments;
          // You logic will come here  
    }); </script>

Try it out, it should work.

试试看,它应该可以工作。

回答by Timothy Macharia

This is how you are going to access the data in your ViewBag. Remember ViewBag data is normally dynamic and can only be accessed at runtime.

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

ViewBag.Appointments = appointments;

After you've set ViewBag data in the controller, retrieve it in your view as shown below.

在控制器中设置 ViewBag 数据后,如下所示在视图中检索它。

@{
    var appointments = ViewBag.Appointments;
}

Then you can loop through the results to get the individual items using a foreach loop.

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

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

Hope this helps!

希望这可以帮助!