r.uters80 says:
string
[] Members = {
"John"
,
"Jane"
,
"Jhon"
};
This is string Array. So its working.
r.uters80 says:
string
[] MembersWithKey = [{
"Firstname"
:
"John"
,
"Lastname"
:
"Doe"
,
"Age"
:
"37"
},{
"Firstname"
:
"Jane"
,
"Lastname"
:
"Doe"
,
"Age"
:
"27"
},{
"Firstname"
:
"Jhon"
,
"Lastname"
:
"Doe"
,
"Age"
:
"41"
}];
This is Json string not an array.
You need to convert the Json string to Generic List collection and then able to loop through each object.
Refer below example.
IndexModel
public class IndexModel : PageModel
{
}
Razor Page
@page
@model RazorKey.Pages.IndexModel
@{
Layout = null;
string[] Members = { "John", "Jane", "Jhon" };
string MembersWithKeyJson = "[{ \"Firstname\":\"John\",\"Lastname\":\"Doe\",\"Age\":\"37\"},{ \"Firstname\":\"Jane\",\"Lastname\":\"Doe\",\"Age\":\"27\"},{ \"Firstname\":\"Jhon\",\"Lastname\":\"Doe\",\"Age\":\"41\"}]";
List<Person> MembersWithKey = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Person>>(MembersWithKeyJson);
}
@functions {
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@foreach (var Person in Members)
{
<button> @Person </button>
}
<hr />
@foreach (Person Person in MembersWithKey)
{
<table>
<tr>
<td>@Person.Firstname</td>
<td>@Person.Lastname</td>
<td>@Person.Age</td>
</tr>
</table>
}
</body>
</html>
Screenshot