ASP.NET MVC 3剃须刀:初始化JavaScript数组 [英] ASP.NET MVC 3 Razor : Initialize a JavaScript array
问题描述
会是什么prefered方式来初始化ASP.NET MVC 3一个JS数组与值我在我的模型/视图模型剃刀?
例如初始化字符串重新presenting日期的数组:
<脚本类型=文/ JavaScript的>
变种activeDates = [2011年7月21日,2011年7月22日];
< / SCRIPT>
与
公共类MyViewModel
{
公共DateTime的[] {ActiveDates获得;组; }
}
我不明白JS和ASP.NET MVC 3剃须刀之间的关系。的JavaScript在客户端运行,无论哪一种技术已用于在服务器上生成的网页。因此,对JavaScript的数组是一个数组。
有几个可能的原因在JavaScript来定义对象的数组
VAR activeDates = ['2011年7月21日,2011年7月22日'];
或
VAR activeDates =新的Array();
activeArrays.push('2011年7月21日');
activeArrays.push('2011年7月22日');
或尚未:
VAR activeDates =新的Array();
activeArrays [0] ='2011年7月21日;
activeArrays [1] ='2011年7月22日;
在日结束所有那些重新present同一阵列。但它的字符串,不是一个数组的日期
如果你想有日期的数组,这里是你能做什么:
VAR activeDates = [
新日期(2011年,6,21,0,0,0,0),
新日期(2011年,6,22,0,0,0,0)
];
现在我可以用你的问题ASP.NET MVC中的唯一关系是,你可能对您的视图模型的一些数组:
公共类MyViewModel
{
公共DateTime的[] {ActiveDates获得;组; }
}
这是你想要的序列化和JavaScript数组操作。在这种情况下,这里的语法:
@model MyViewModel
<脚本类型=文/ JavaScript的>
VAR activeDates = @ Html.Raw(Json.En code(Model.ActiveDates));
< / SCRIPT>
现在由于道路的日期时间字段中的JSON序列化的.NET你最终会在生成的HTML如下:
VAR activeDates =\\ /日期(1309471200000)\\ /,\\ /日期(1311199200000)\\ /];
如果你想这个字符串数组转化为实际的javascript日期数组:
VAR日期= $ .MAP(activeDates,函数(日期,指数){
日期= date.replace('/日期(','').replace(')/','');
返回新的日期(parseInt函数(日期));
});
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 ?
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; }
}
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.
There are a couple of possibilities to define arrays of objects in 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:
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:
@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:
var activeDates = ["\/Date(1309471200000)\/","\/Date(1311199200000)\/"];
and if you wanted to convert this array of strings into an array of actual javascript dates:
var dates = $.map(activeDates, function(date, index) {
date = date.replace('/Date(', '').replace(')/', '');
return new Date(parseInt(date));
});
这篇关于ASP.NET MVC 3剃须刀:初始化JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!