0

The following classes are used to define an ApplicationUser with nested objects using composition, while also providing the view with a nested property.

When visiting ~/Views/Type1/Index.cshtml, the following error is output:

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

When looking at the property in the database, the Type1FK is set within the user's record, so I'm not sure why it is not accessible through the controller/view. How can a nested property be set and accessed correctly?

AppUser.cs

using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
    public class AppUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [ForeignKey("Type1FK")]
        public Type1 Type1 { get; set; }
    }
}

Type1.cs

namespace MyApp.Models
{
    public class Type1
    {
        public int Type1Id { get; set; }
        public string Property1 { get; set; }
        public AppUser AppUser { get; set; }
    }
}

Type1Controller.cs

using System.Threading.Tasks;
using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace MyApp.Controllers
{
    [Authorize(Roles = "Type1")]
    public class Type1Controller : Controller
    {
        private UserManager<AppUser> userManager;

        public Type1Controller (UserManager<AppUser> _userManager)
        {
            userManager = _userManager;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            AppUser user = await userManager.GetUserAsync(HttpContext.User);
            ViewBag.name = user.Type1.Name;
            return View("~/Views/Type1/Index.cshtml");
        }
    } 
}

~/Views/Type1/Index.cshtml

<div class="container-fluid">
    <h1>@ViewBag.name</h1>
</div>
crayden
  • 2,130
  • 6
  • 36
  • 65
  • `Type1` class is not a static class, sounds like you must instantiate it first in `AppUser` constructor e.g. `this.Type1 = new Type1();`. – Tetsuya Yamamoto Nov 23 '18 at 04:07
  • Interesting.The error has disappered, but the

    tag is empty. Why?
    – crayden Nov 23 '18 at 04:14
  • The `ViewBag.name` is empty because you're never assign `Type1.Name` after instantiation and renders empty string. Can you show what `user` contains when retrieving user with `GetUserAsync` (I mean value of each `AppUser` properties)? – Tetsuya Yamamoto Nov 23 '18 at 04:16
  • What is the quickest way to view all of the values? And the property is set when I create the user. I can see it in the database, I even just recreated the database, and created the initial migration. – crayden Nov 23 '18 at 04:17
  • If the dbcontext is not eager loading the user records, that table won't get loaded. – Brendan Green Nov 23 '18 at 04:23
  • Take a look for [this issue](https://stackoverflow.com/questions/46136037/usermanager-getuserasyncuser-returns-null) if `GetUserAsync()` returns null. Also consider to review eager loading mechanism of `IdentityUser`. – Tetsuya Yamamoto Nov 23 '18 at 04:25
  • How do I turn on the eager loading for the user records? – crayden Nov 23 '18 at 04:29
  • take a look for the word `Including` – jazb Nov 23 '18 at 05:01
  • It isn't available on the userManager.Users. – crayden Nov 23 '18 at 05:06

0 Answers0