Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

c# - How to get the values from the Model and into Views Javascript (Visual Studio 2015 mvc)

This is my Models code:

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

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();
    }
}

Basically I want to obtains the values from list year from within the Models to the javascript which is seen declared in my views.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
});

Side note: Recommend you follow normal naming conventions and name your property Years - plural - (not year) and your class GraphUser


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...