-2

I was trying to make a list of all the users and I tried this code and this one is giving me an exception of NullReferenceException I tried to type it manually and then when it was making trouble I tried to scaffold it using List T4 Template but it still didnt work out

@model IEnumerable<Aroma.Models.user>

@{
    ViewBag.Title = "ManageUsers";
}

<h2>ManageUsers</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.pass)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.usertype.name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.pass)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.usertype.name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
            @Html.ActionLink("Details", "Details", new { id=item.id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.id })
        </td>
    </tr>
}

</table>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Extensive detail is needed to narrow down the question as much as possible. To prevent duplicate questions and so on. That being said, is the model properly initialized in the controller? – Dortimer Mar 14 '19 at 17:25
  • model is properly initialized i have used the same table to login and sign up the account – Shehzad Ahmed Mar 14 '19 at 17:29
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch Mar 14 '19 at 17:34
  • 1
    Check out this post: https://stackoverflow.com/a/17037925/2720343 if that doesn't work I'd suggest setting breakpoints in the controller and inspecting the data before it reaches the view. Even if you used the same table for logging in and signing up, that doesn't mean that the same will be true for this view. – Dortimer Mar 14 '19 at 17:34
  • No it's not a duplicate of that I've already seen it, that's a different question. – Shehzad Ahmed Mar 14 '19 at 17:38

1 Answers1

1

You are trying to display the name of the property on the foreach iteration. That's probably it, since I dont think it has the necessary Attribute to do so. You should just do:

@foreach (var item in Model) {
<tr>
    <td>
        @item.username
    </td>
    <td>
        @item.pass
    </td>
    <td>
        @item.email
    </td>
    <td>
        @item.usertype.name
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.id }) |
        @Html.ActionLink("Details", "Details", new { id=item.id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.id })
    </td>
</tr>

}

davi.in
  • 21
  • 7