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

后端 未结 1 1754
春和景丽
春和景丽 2020-12-11 22:58

This is my Models code:

public class graph_user
{
    public  List year { get; set; }
}

This is my Views code side:

         


        
相关标签:
1条回答
  • 2020-12-11 23:06

    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

    0 讨论(0)
提交回复
热议问题