Access Nested Property in ASP.NET 2.1 ApplicationUser
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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>
c# asp.net-mvc asp.net-identity
|
show 4 more comments
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>
c# asp.net-mvc asp.net-identity
Type1class is not a static class, sounds like you must instantiate it first inAppUserconstructor e.g.this.Type1 = new Type1();.
– Tetsuya Yamamoto
Nov 23 '18 at 4:07
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4:14
TheViewBag.nameis empty because you're never assignType1.Nameafter instantiation and renders empty string. Can you show whatusercontains when retrieving user withGetUserAsync(I mean value of eachAppUserproperties)?
– Tetsuya Yamamoto
Nov 23 '18 at 4: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 4:17
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23
|
show 4 more comments
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>
c# asp.net-mvc asp.net-identity
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>
c# asp.net-mvc asp.net-identity
c# asp.net-mvc asp.net-identity
asked Nov 23 '18 at 4:02
craydencrayden
4231622
4231622
Type1class is not a static class, sounds like you must instantiate it first inAppUserconstructor e.g.this.Type1 = new Type1();.
– Tetsuya Yamamoto
Nov 23 '18 at 4:07
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4:14
TheViewBag.nameis empty because you're never assignType1.Nameafter instantiation and renders empty string. Can you show whatusercontains when retrieving user withGetUserAsync(I mean value of eachAppUserproperties)?
– Tetsuya Yamamoto
Nov 23 '18 at 4: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 4:17
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23
|
show 4 more comments
Type1class is not a static class, sounds like you must instantiate it first inAppUserconstructor e.g.this.Type1 = new Type1();.
– Tetsuya Yamamoto
Nov 23 '18 at 4:07
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4:14
TheViewBag.nameis empty because you're never assignType1.Nameafter instantiation and renders empty string. Can you show whatusercontains when retrieving user withGetUserAsync(I mean value of eachAppUserproperties)?
– Tetsuya Yamamoto
Nov 23 '18 at 4: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 4:17
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23
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 4:07
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 4:07
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4:14
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4: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 4:16
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 4: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 4:17
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 4:17
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440516%2faccess-nested-property-in-asp-net-2-1-applicationuser%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440516%2faccess-nested-property-in-asp-net-2-1-applicationuser%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Type1class is not a static class, sounds like you must instantiate it first inAppUserconstructor e.g.this.Type1 = new Type1();.– Tetsuya Yamamoto
Nov 23 '18 at 4:07
Interesting.The error has disappered, but the <h1></h1> tag is empty. Why?
– crayden
Nov 23 '18 at 4:14
The
ViewBag.nameis empty because you're never assignType1.Nameafter instantiation and renders empty string. Can you show whatusercontains when retrieving user withGetUserAsync(I mean value of eachAppUserproperties)?– Tetsuya Yamamoto
Nov 23 '18 at 4: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 4:17
If the dbcontext is not eager loading the user records, that table won't get loaded.
– Brendan Green
Nov 23 '18 at 4:23