0

I'm trying to show the data from a json file in a web page but I get this error.
This is my test.cshtml, the error is on the @foreach line:

@model API_STA_1.Classes.Description

@{
    ViewData["description"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>

            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.projects)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @Html.ActionLink("Update", "Update", new { /*id = item.PrimaryKey*/}) |
                    @Html.ActionLink("Delete", "Delete", new { /*id = item.PrimaryKey*/}) |
                </td>
            </tr>
        }
    </tbody>
</table>

enter image description here

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
T.v.A.
  • 49
  • 1
  • 9

1 Answers1

2

You are not passing Model object to the view from your controller action.

you will need to pass it back to the view.

See:

var description = JsonConvert.DeserializeObject<List<Description>>(json);

return View(description);

Secodnly you don't need to specify the view fully qualified path with name as by convention it will look in Views folder a folder named Test and then view file named test.cshtml based on your controller class name and action name.

UPDATE: OP was getting error about view not found so the following worked for him:

return View("test/test.cshtml", description);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • So i changed it to `description` but now i get an error because it's looking for test.cshtml in the views folder but i don't have a views folder. https://imgur.com/a/KDYZEBT – T.v.A. Apr 23 '20 at 14:44
  • 1
    @T.v.A. then try what you did already: `return View("test/test.cshtml",description);` – Ehsan Sajjad Apr 23 '20 at 14:58
  • That works! Thank you. I'm new to this and this was a big help! – T.v.A. Apr 23 '20 at 15:03
  • @T.v.A. no problem and welcome, the code in the answer should work, but you probably have something missing in startup so that's why we need to explicitly specify the path of view otherwise it works by convention – Ehsan Sajjad Apr 23 '20 at 15:05