简体   繁体   English

如何在我的视图中访问C#数组?

[英]How do I access my C# Array in my View?

I have an array in my Controller that I'm passing over to my View, and (temporarily) I'm trying to console.log some of the values in the array (just to make sure I passed the array over correctly.) 我在Controller中有一个数组要传递给View,并且(暂时)在尝试console.log数组中的某些值(以确保我正确传递了该数组。)

I'm trying to send it over with ViewData and access it through Razor code, but Razor won't allow me to do any javascript functions (such as console.log ). 我试图通过ViewData发送它并通过Razor代码访问它,但是Razor不允许我执行任何javascript函数(例如console.log )。

I've tried a few different solutions but can't come up with anything that works, this is the solution that got me the closest: 我尝试了几种不同的解决方案,但是无法提出任何可行的解决方案,这是使我最接近的解决方案:

View 视图

function logValues()
{
    console.log("test");      
    @{var capacity = ViewData["Capacity"] as string[]; }
    for (var i = 0; i < 5; i++)
    {
        console.log(capacity[i]);
    }
}

Controller 调节器

var uspCapacity = from d in db.uspGetEventKillSheetDetailedReport(option, date)
                  select new
                  {
                     d.Capacity
                  };
var uspCapacityList = uspCapacity.ToList();
ViewData["Capacity"] = uspCapacityList;

The console will successfully log "Test" but cannot access my capacity variable.. because I'm defining it in Razor and trying to access it outside of Razor. 控制台将成功记录“测试”,但无法访问我的容量变量..因为我正在Razor中定义它并尝试在Razor之外访问它。 How can I access it with Javascript commands? 如何使用Javascript命令访问它?

You can convert the C# array to JSON and assign this to a JS variable: 您可以将C#数组转换为JSON并将其分配给JS变量:

<script>
    var capacity = @Html.Raw(Json.Encode(ViewData["Capacity"]));

    // now you can use capacity like any other JS variable...
    for (var i = 0; i < capacity.length; i++) {
        console.log(capacity[i]);
    }
</script>

Json.Encode is defined in System.Web.Helpers and should be available in every MVC project. Json.EncodeSystem.Web.Helpers定义,并且应该在每个MVC项目中都可用。

Your current server code is setting a collection of anonymous objects to the viewdata dictionary. 您当前的服务器代码正在将匿名对象的集合设置为viewdata字典。 It is not an array of strings. 它不是字符串数组。

ViewData["Capacity"] = (from d in db.uspGetEventKillSheetDetailedReport(option, date)
                       select  d.Capacity).ToList();

This should work, assuming this code is in a razor file 假设此代码在剃刀文件中,这应该可以工作

function logValues()
{
    console.log("test");      
    var capacity = @Html.Raw(JsonConvert.SerializeObject(ViewData["Capacity"]))  
    for (var i = 0; i < 5; i++)
    {
        console.log(capacity[i]);
    }
}

When razor executes the above code, it will serialize the string list we set to Viewdata dictionary to a string for the array initialization(ex: ["Java","Swift","SQL","PHP","Ruby"] ) and js will set that value to the capacity variable 当razor执行上述代码时,它将序列化我们设置为Viewdata字典的字符串列表序列化为用于数组初始化的字符串(例如: ["Java","Swift","SQL","PHP","Ruby"] )和js将值设置为capacity变量

JsonConvert is defined in the Newtonsoft.Json namespace. JsonConvertNewtonsoft.Json命名空间中定义。 So you need to import that to your razor view. 因此,您需要将其导入到剃刀视图中。

Try this: 尝试这个:

function logValues()
{
    console.log("test");      
    @{var capacity = (List<int>)ViewData["Capacity"]; }
    @foreach (var s in capacity)
    {
        <text>console.log(@s)</text>
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM