I am trying to read a .json File and return the value as a table in asp .net Core 3.1! I am new at working with asp .net core, so please help me from a beginners perspective.
Consider the code snippet below. My problem is that I cannot pass the values from the controller to the view.
I encounter this exception:
An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type '.Sample.Models.Mstype', but this ViewDataDictionary instance requires a model item of type 'Sample.Models.Mstypes'.
Controller
public IActionResult Run()
{
...
var webClient = new WebClient();
var json = webClient.DownloadString(@"/app/results/type.json");
var mstypes = JsonConvert.DeserializeObject<Mstype>(json);
return View(mstypes);
}
Mstype.cs
public int CIS { get; set; }
public int PPMS { get; set; }
public int RRMS { get; set; }
public int SPMS { get; set; }
public int not_sure { get; set; }
Mstypes.cs
public class Mstypes
{
public IList<Mstype> mstypes { get; set; }
}
View
@model Fed_PiP.Models.Mstypes
@{
ViewData["Title"] = "Run";
}
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(Model => Model.mstypes[0].CIS)
</th>
<th>
@Html.DisplayNameFor(Model => Model.mstypes[0].PPMS)
</th>
<th>
@Html.DisplayNameFor(Model => Model.mstypes[0].RRMS)
</th>
<th>
@Html.DisplayNameFor(Model => Model.mstypes[0].SPMS)
</th>
<th>
@Html.DisplayNameFor(Model => Model.mstypes[0].not_sure)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@Model.mstypes.FirstOrDefault().CIS
</td>
<td>
@Model.mstypes.FirstOrDefault().PPMS
</td>
<td>
@Model.mstypes.FirstOrDefault().RRMS
</td>
<td>
@Model.mstypes.FirstOrDefault().SPMS
</td>
<td>
@Model.mstypes.FirstOrDefault().not_sure
</td>
</tr>
</tbody>
</table>
and finally json file
{"CIS":11,"PPMS":12,"RRMS":6,"SPMS":11,"not_sure":10}
also, I have a question is there any way to make a dynamic table from the static JSON files? I mean without defining the model or ViewModel just reading and returning the values from the static file to the tables?

JsonConvert.DeserializeObject<Mstypes>(json)