gpt4 book ai didi

c# - MVC5 : Object reference not set to an instance of an object?

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:59 27 4
gpt4 key购买 nike

我不是很明白为什么我的 MVC5/EF6/Code First 迁移应用程序会出现此错误。我正在尝试(在我的用户列表中)显示组织的 [名称] 或用户当前关联的发起人的 [名称]。我在我的 IdentityModels.cs 中定义了以下属性:

    [GridColumn(Title = "Org.", SortEnabled = true, Width = "100")]
public int? MemberOrgId { get; set; }

[NotMappedColumn]
public int? SponsorOrgId { get; set; }

[NotMappedColumn]
public virtual MemberOrganizations Organization { get; set; }

[NotMappedColumn]
public virtual SponsorOrganizations Sponsor { get; set; }

MemberOrganizations.cs:

    [Required]
[Display(Name = "Organization")]
public string Name { get; set; }

[Display(Name = "Users:")]
public virtual ICollection<ApplicationUser> Users { get; set; }

SponsorOrganizations.cs:

    [Required]
[Display(Name="Name")]
public string Name { get; set; }

[Display(Name = "Users:")]
public virtual ICollection<ApplicationUser> Users { get; set; }

这是我目前正在使用的索引 View :

@using GridMvc.Html
@model IEnumerable<PROJECTS.Models.ApplicationUser>

@{
ViewBag.Title = "View Users";
Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";

}

<div class="overflowPrevention">
<!-- Grid.MVC Code -->
<div>

@Html.Grid(Model).Columns(columns =>
{
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => Html.ActionLink("Edit", "Edit", "UserManagement", new { id = o.Id }, null)).SetWidth(15);
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Name).Titled("Name").Sortable(true).Filterable(true).SetWidth(100);
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Position).Titled("Position").Sortable(true).Filterable(true).SetWidth(50);
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Email).Titled("Email").Sortable(true).Filterable(true).SetWidth(100);
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.EmailConfirmed.ToString()).Titled("Confirmed").Sortable(true).Filterable(true).SetWidth(100);
// VALUE TRYING TO RENDER
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Organization.ToString()).Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);
// BELOW CORRECTLY RENDERS THE MEMBER ORG ID
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.MemberOrgId.ToString()).Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);
// BELOW CORRECTLY RENDERS THE SPONSOR ID
columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.SponsorOrgId.ToString()).Titled("Sponsor").Sortable(true).Filterable(true).SetWidth(25);
}).WithPaging(5)
</div>
</div>

我之前尝试过 columns.Add().Encoded(false).Sanitized(false).RenderValueAs(f => Html.Raw(s => s.Organization.Name));但是段s => s.Organization.Name一直导致 Cannot convert lambda expression to type 'object' because it is not a delegate type .

据我所知,我的代码行 columns.Add().Encoded(false).Sanitized(false).RenderValueAs(o => o.Organization.ToString()).Titled("MemOrg.").Sortable(true).Filterable(true).SetWidth(25);应该能够正确呈现 [Name](在模型中指定显示为“组织”),但是当加载 View 时我收到 Object reference not set to an instance of an object.

谁能解释我做错了什么?

编辑:我发现 [User.Organization] (MemberOrganizations.Name) & [User.Sponsor] (SponsorOrganizations.Name) 确实从我的 Controller 传递为 null。为了解决这个问题,我没有尝试循环遍历我的 <ApplicationUser> 列表。的并查询适当的表以填充当前的空值。

这是我目前所拥有的:

    // GET: Admin/UserManagement
public async Task<ActionResult> Index()
{
ViewBag.headerTitle = "User Management";
ViewBag.showCreateButton = true;
ViewBag.createUrl = "/Admin/UserManagement/Create";
ViewBag.createText = " Create a New User";
List<ApplicationUser> model = UserManager.Users.ToList();
foreach (ApplicationUser user in model)
{
if (user.MemberOrgId != null)
{
user.Organization = db.MemberOrganizations.Find(user.Organization);

}
if (user.SponsorOrgId != null)
{
user.Sponsor = db.SponsorOrganizations.Find(user.SponsorOrgId);
}
}
return View(model);
}

中断第一个用户,user.Sponsor属性(property)最终成为:

 ?user.Sponsor
{System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A}
[System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A]: {System.Data.Entity.DynamicProxies.SponsorOrganizations_F2A27693F33AFF36C814F6FFA2CE63AD656938244F599B7D1AB2ED80CC26EF9A}
City: null
Country: null
Enabled: true
FacebookUrl: "https://www.facebook.com/sponsor1"
Id: 1
LogoSrc: "Sponsor1.jpg"
Name: "Sponsor1"
OwnerId: "1d720c20-a508-4880-a5e6-80af5f2f8caf"
OwnerName: "Sponsor Primary"
ProfilePictureUrl: "sponsorprofilepicture/Sponsor1.jpg"
SponsorServiceCategory: Count = 11
State: null
TwitterUrl: "https://twitter.com/sponsor1"
Users: Count = 0
WebsiteUrl: "http://www.sponsor1.com"

我如何获得 Name属性(property)具体来自db.SponsorOrganizations.Find(user.SponsorOrgId)进入user.Sponsor

最佳答案

IdentityModels.cs 中的外键属性未正确命名。外键属性必须与导航属性的名称匹配,否则您需要使用 [ForeignKey] 属性。

应该是:

[GridColumn(Title = "Org.", SortEnabled = true, Width = "100")]
public int? OrganizationId { get; set; }

[NotMappedColumn]
public virtual MemberOrganizations Organization { get; set; }

或:

[GridColumn(Title = "Org.", SortEnabled = true, Width = "100")]
public int? MemberOrgId { get; set; }

[ForeignKey("MemberOrgId")
[NotMappedColumn]
public virtual MemberOrganizations Organization { get; set; }

或:

[ForeignKey("Organization")
[GridColumn(Title = "Org.", SortEnabled = true, Width = "100")]
public int? MemberOrgId { get; set; }

[NotMappedColumn]
public virtual MemberOrganizations Organization { get; set; }

SponsorOrganizations 也可能存在一些问题,因为您已将外键标记为未映射。

关于c# - MVC5 : Object reference not set to an instance of an object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24193299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com