如何从模型和为视图的Javascript得到的值(Visual Studio的2015年,MVC) [英] How to get the values from the Model and into Views Javascript (Visual Studio 2015 mvc)

查看:93
本文介绍了如何从模型和为视图的Javascript得到的值(Visual Studio的2015年,MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型code:

This is my Models code:

public class graph_user
{
    public  List<int> year { get; set; }
}

这是我的看法code面:

This is my Views code side:

@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的&LT; graph_user&GT; (您目前的定义不会编译)

Assuming your model is actually @model graph_user and not @model IEnumerable<graph_user> (your current definition would not compile)

您可以使用模型集合分配给你一个javascript变量

You can assign you models collection to a javascript variable using

var yearList = @Html.Raw(Json.Encode(Model.year))

然后,使用迭代它

Then iterate over it using

$.each(yearList, function(index, item) {
  var year = item;
  // do something with it
});

边注:建议您按照正常的命名约定并命名你的财产 - 复数 - (未)和你的类 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屋!

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