-4

In my situation, how can I show a list into a table? In my view i've tried this

     @foreach (var AllUsers in Model.AllUsers)
            {
                <td>@AllUsers.Name</td>
                <td>@AllUsers.Email</td>
                <td>@AllUsers.IsActive</td>
            }

but I'm getting this error

NullReferenceException: Object reference not set to an instance of an object.

Also here is my controller:

 public IActionResult Users()
    {
        using (var aplicationDbContext = new ApplicationContext())
        {
            ApplicationUser user = new ApplicationUser();

            var AllUsers = aplicationDbContext.ApplicationUsers.AsNoTracking().FirstOrDefault(m => m.Id==m.Id);
            user.AllUsers = AllUsers.AllUsers;
            if (AllUsers == null)
            {
                return NotFound();
            }
        }
        return View(AllUsers);
    }
  • I wonder How did your code compile with above scenario – Manoz Jun 06 '18 at 10:55
  • @Liam I've already read that thread and tried to fix but still the same error... – Dorin Munreanu Jun 06 '18 at 10:56
  • @Manoj what do you mean ? – Dorin Munreanu Jun 06 '18 at 10:56
  • the following could be null here `aplicationDbContext`, `aplicationDbContext.ApplicationUsers`, the result of `aplicationDbContext.ApplicationUsers.AsNoTracking()` and/or `aplicationDbContext.ApplicationUsers.AsNoTracking().FirstOrDefault(m => m.Id==m.Id)` – Liam Jun 06 '18 at 10:57
  • What is the ModelType of the view? Which line of code gives the error? – Chetan Ranpariya Jun 06 '18 at 10:58
  • 2
    That seems unlikely @DorinMunreanu, because if you'd read and understood that duplicate you would have considerably more null checking in your code. – Liam Jun 06 '18 at 10:58
  • 2
    If you really have read the linked duplicate, you will have seen that you have to debug your code to find where the actual null reference is. – stuartd Jun 06 '18 at 10:58
  • 2
    `.FirstOrDefault(m => m.Id==m.Id);` - that looks wrong. – stuartd Jun 06 '18 at 10:59
  • @ChetanRanpariya "ApplicationUser" – Dorin Munreanu Jun 06 '18 at 10:59
  • @stuartd why is that can you explain please ? – Dorin Munreanu Jun 06 '18 at 10:59
  • 1
    Well, a) it appears you want all users, but you're only selecting one and b) you don't need to check that it's ID is equal to itself. – stuartd Jun 06 '18 at 11:00
  • `m.Id==m.Id` is **always** going to return `true`... – Liam Jun 06 '18 at 11:01
  • 1
    You have `Model.AllUsers` is null. Because you are not setting that in the controller. To me it looks like you should have `return View(user);` instead of `return View(AllUsers);` – Chetan Ranpariya Jun 06 '18 at 11:02
  • your code have `ApplicationUser user = new ApplicationUser();` `user.AllUsers = AllUsers.AllUsers;`.... what's that? – Chetan Ranpariya Jun 06 '18 at 11:03
  • @ChetanRanpariya notice how that `user` variable is scoped ... – rene Jun 06 '18 at 11:04
  • @ChetanRanpariya on the line ´AllUsers.AllUsers`... I was testing something just ignore that line – Dorin Munreanu Jun 06 '18 at 11:05
  • @rene Scoped variables can be passed as a model to the view. Dorin, if you debug your code properly, `Model.AllUsers` property is null. if you can confirm that it's not then there is some other issue.... Which line of code gives you the error? – Chetan Ranpariya Jun 06 '18 at 11:07
  • 2
    Strange... an "ApplicationUser" class, which logically sounds like it would contain details of _one_ user, then has a property "AllUsers" which contains a list of everyone. It doesn't logically make much sense. Why do you have this confusing object structure? – ADyson Jun 06 '18 at 11:08
  • @ADyson on my user model I just created a list where every user is saved, isn't that the right way to do this ? – Dorin Munreanu Jun 06 '18 at 11:10
  • Not really, no, that's my point. Why would an object called "user" which sounds like it means a single user then contain a list of users? It's illogical and confusing. You can just use a list directly as your model. I'll write an answer which might help you. – ADyson Jun 06 '18 at 11:11
  • @ADyson please do, if that help's I'll accept it as an answer to my question – Dorin Munreanu Jun 06 '18 at 11:13

1 Answers1

1

Frankly your object structure doesn't make much sense and can probably be simplified. An "ApplicationUser" class, which logically sounds like it would contain details of one user, then has a property "AllUsers" which contains a list of everyone. It's confusing.

You also have a couple of other logical errors in your code as mentioned in the comments - only returning one row into AllUsers, and returning the wrong object as your model.

So...I think you can do it like this with less fuss:

Controller:

public IActionResult Users()
{
    using (var aplicationDbContext = new ApplicationContext())
    {
       var AllUsers = aplicationDbContext.ApplicationUsers.toList(); //return all users not just the first
       return View(AllUsers);
    }
}

View:

@model List<ApplicationUser>

<table>
@foreach (var usr in Model)
{
  <tr>
    <td>@usr.Name</td>
    <td>@usr.Email</td>
    <td>@usr.IsActive</td>
  </tr>
}
</table>
ADyson
  • 44,946
  • 12
  • 41
  • 55
  • Getting an error on `@Model List` "The type namespace name "ApplicationUser" could not be found (are you missing a using directive or an assembly reference ?) – Dorin Munreanu Jun 06 '18 at 11:22
  • 1
    Assuming you haven't googled this error yourself yet and solved it, then I'll tell you you need to use the fully qualified namespace of the class to declare it. I don't know which namespace it's in, so it's hard to give you a specific line of code. But something in the pattern `YourApplication.SomeNamespace.SomeSubNamespace.ApplicationUser`. Looking at your code it's probably the namespace where your Entity Framework classes are held. You probably have a `using` statement for that namespace in your controller already. – ADyson Jun 06 '18 at 12:25
  • I have tried this `@model List` but still getting an error"'List' does not contain a definition for 'AllUsers' and no extension method 'AllUsers' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)" – Dorin Munreanu Jun 06 '18 at 13:09
  • 1
    I have already fixed the problem, it was my problem, Thanks! – Dorin Munreanu Jun 06 '18 at 13:30