ASP.NET MVC - 如何将数组传递给视图? [英] ASP.NET MVC - How to pass an Array to the view?

查看:30
本文介绍了ASP.NET MVC - 如何将数组传递给视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里苦苦挣扎,想找到一种简单的方法将数组从控制器传递到 ASP.NET MVC 框架上的视图.

I'm struggling myself here, to find a easy way to pass an array from the controller to the view on ASP.NET MVC framework.

所以在我的控制器中我会有类似的东西:

so in my controller I would have something like:

public class HomeController : ApplicationController
{   
    public ActionResult Index()
    {
        string[] myArray = { "value01", "value02", "value03"};
        ViewData["passedArray"] = myArray;
        return View();
    }
}

所以在我看来,我只需调用 ViewData["passedArray"] 并在其上运行一个循环.

so in my view I would have just a call to ViewData["passedArray"] and run a loop on it.

但显然 ViewData 被视图作为 System.String 接收,可能是因为 Array DataType 上的声明,但不幸的是我不知道如何在不创建数百万行代码的情况下正确且简单地传递它.

But apparently the ViewData is being received by the view as System.String, probably because of the declaration on the Array DataType, but unfortunately I don't know how to pass it properly and simply without create millions of code lines.

如果有人能帮助我就好了.

It would be fantastic if one could help me.

提前致谢

推荐答案

需要在View中进行投射

You need to cast in the View

<% var myArray = (string[])ViewData["passedArray"]; %>

这篇关于ASP.NET MVC - 如何将数组传递给视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆