I have a embeded C# code into my HTML file. Which first deserializes the JSON file:
[
{
"Subject": "TEST APP",
"AppScores": [
{
"Season": "AAAAAAAAAAAAAAAAAAAA",
"year": "1"
},
{
"Season": "BBBBBBBBBBBBBBBBBBBBB",
"year": "2"
}
]
},
{
"Subject": "TEST APP2",
"AppScores": [
{
"Season": "CCCCCCCCCCC",
"year": "3"
},
{
"Season": "DDDDDDDDDDDDDDDDD",
"year": "4"
}
]
}
]
Can anyone help to show this on my page as it doesn't seem to work.
For the moment I am trying to get the data within the Response.Write
to display onto the webpage. I have chosen to do this on the HTML page as I am puzzled on how to do it within MVC.
Once this is done I am then going to use the variable from Response.Write / for each to pass into a Bar Chart. Can anyone help as I am really struggling.
Thanks in advance.
I have updated your code to display the data in line. I have replaced Response.Write
with Html.Raw
. This will help in displaying your text in line.
@{
ViewBag.Title = "SchoolSubject";
}
<h2>SchoolSubject</h2>
@{
string json = File.ReadAllText("JSON.json");
var root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<MyTest.Models.SchoolSubject>>(json);
foreach (var subject in root)
{
@Html.Raw(subject.Subject);
foreach (var item in subject.AppScores)
{
@Html.Raw(item.Season);
@Html.Raw(item.year);
}
}
}
Lets say the class is in SchoolSubject.cs file as below
namespace MyTest.Models
{
public class SchoolSubjectAppScore
{
public string Season { get; set; }
public string year { get; set; }
}
public class SchoolSubject
{
public SchoolSubject() { this.AppScores = new List<SchoolSubjectAppScore>(); }
public string Subject { get; set; }
public List<SchoolSubjectAppScore> AppScores { get; set; }
}
}
Suggested Html.Raw as this is just temporary. Otherwise you may have to make sure of the validity of the data passed to it.