ASP.NET MVC 3 Razor:初始化一个 JavaScript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6777385/
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-23 23:08:43  来源:igfitidea点击:

ASP.NET MVC 3 Razor : Initialize a JavaScript array

javascriptarraysasp.net-mvc-3razor

提问by Matthieu

What would be the prefered way to initialize a JS array in ASP.NET MVC 3 with Razor with a value I have in my model/view model ?

在 ASP.NET MVC 3 中使用 Razor 初始化 JS 数组的首选方法是什么,我的模型/视图模型中有一个值?

For example to initialize an array of strings representing dates :

例如初始化表示日期的字符串数组:

<script type="text/javascript">
    var activeDates = ["7-21-2011", "7-22-2011"];
</script>

with

public class MyViewModel
{    
  public DateTime[] ActiveDates { get; set; }
}

回答by Darin Dimitrov

I don't quite understand the relation between JS and ASP.NET MVC 3 Razor. Javascript runs on the client side no matter which technology has been used on the server to generate the page. So on javascript an array is an array.

我不太明白 JS 和 ASP.NET MVC 3 Razor 之间的关系。无论在服务器上使用哪种技术生成页面,Javascript 都在客户端运行。所以在 javascript 上,一个数组就是一个数组。

There are a couple of possibilities to define arrays of objects in javascript

在 javascript 中定义对象数组有几种可能性

var activeDates = [ '7-21-2011', '7-22-2011' ];

or:

或者:

var activeDates = new Array();
activeArrays.push('7-21-2011');
activeArrays.push('7-22-2011');

or yet:

或者:

var activeDates = new Array();
activeArrays[0] = '7-21-2011';
activeArrays[1] = '7-22-2011';

At th end all those represent the same array. But it is an array of strings, not dates.

最后,所有这些都代表相同的数组。但它是一个字符串数组,而不是日期

If you wanted to have an array of dates, here's what you could do:

如果你想要一个日期数组,你可以这样做:

var activeDates = [ 
    new Date(2011, 6, 21, 0, 0, 0, 0), 
    new Date(2011, 6, 22, 0, 0, 0, 0) 
];

Now the only relation I can see with your question to ASP.NET MVC is that you probably have some array on your view model:

现在,我可以看到您对 ASP.NET MVC 的问题的唯一关系是您的视图模型上可能有一些数组:

public class MyViewModel
{
    public DateTime[] ActiveDates { get; set; }
}

that you wanted to serialize and manipulate in a javascript array. In this case here's the syntax:

您想在 javascript 数组中序列化和操作。在这种情况下,语法如下:

@model MyViewModel
<script type="text/javascript">
    var activeDates = @Html.Raw(Json.Encode(Model.ActiveDates));
</script>

Now because of the way DateTime fields are JSON serialized in .NET you will end up with the following in the generated HTML:

现在,由于 DateTime 字段在 .NET 中以 JSON 序列化的方式,您将在生成的 HTML 中得到以下内容:

var activeDates = ["\/Date(1309471200000)\/","\/Date(1311199200000)\/"];

and if you wanted to convert this array of strings into an array of actual javascript dates:

如果您想将此字符串数组转换为实际 javascript 日期数组:

var dates = $.map(activeDates, function(date, index) {
    date = date.replace('/Date(', '').replace(')/', '');  
    return new Date(parseInt(date));
});

回答by Buildstarted

I just happen to do this yesterday and came up with this solution (if you want it to look like the output you have in your question - I was using this with jqplot):

我昨天碰巧这样做并提出了这个解决方案(如果你希望它看起来像你在问题中的输出 - 我在jqplot中使用它):

<script type="text/javascript">
    var activeDates = ['@Html.Raw(string.Join("', '", @ActiveDates.Select(f => string.Format("{0:MM-dd-yyyy}", f)).ToArray()))']
</script>