如何从模型中获取值并进入视图 Javascript (Visual Studio 2015 mvc) [英] How to get the values from the Model and into Views Javascript (Visual Studio 2015 mvc)
本文介绍了如何从模型中获取值并进入视图 Javascript (Visual Studio 2015 mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的模型代码:
public class graph_user
{
public List<int> year { get; set; }
}
这是我的视图代码端:
@model IEnumerable testing.Models.graph_user
function generateChart() {
var data = new google.visualization.DataTable();
for (i = 0; i < year.length; i++) {
data.addColumn('string',);
data.addRow();
}
}
基本上我想从模型中获取列表年份中的值到在我的视图中声明的javascript.
Basically I want to obtains the values from list year from within the Models to the javascript which is seen declared in my views.
推荐答案
假设您的模型实际上是 @model graph_user
而不是 @model IEnumerable
(您当前的定义不会编译)
Assuming your model is actually @model graph_user
and not @model IEnumerable<graph_user>
(your current definition would not compile)
您可以使用
var yearList = @Html.Raw(Json.Encode(Model.year))
然后使用
$.each(yearList, function(index, item) {
var year = item;
// do something with it
});
旁注:建议您遵循正常的命名约定并将您的财产命名为 Years
- 复数 - (不是 year
)和您的类 GraphUser
Side note: Recommend you follow normal naming conventions and name your property Years
- plural - (not year
) and your class GraphUser
这篇关于如何从模型中获取值并进入视图 Javascript (Visual Studio 2015 mvc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文