【发布时间】:2016-04-01 03:56:57
【问题描述】:
我的模型是这样的:
- Goup.cs
- GroupUser(数据透视表)
- ApplicationUser(用户)-> 4. Profile
现在我想在用户属于该组时在详细信息页面上显示配置文件中的数据。我这样做是这样的:
private IEnumerable<GroupUser> GetUsers(int groupId)
{
IEnumerable<GroupUser> model = null;
if(groupId == 0)
{
model = _kletsContext.GroupUser.OrderByDescending(o => o.GroupId).AsEnumerable();
}
else
{
model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User.Profile).OrderByDescending(o => o.GroupId).AsEnumerable();
}
return model;
}
如果我只想使用以下代码显示 UserId,...(因此数据透视表中的数据),这可行:
@model IEnumerable<App.Models.GroupUser>
@if(Model != null && Model.Count() > 0)
{
@foreach(var user in Model)
{
@user.UserId</h2>
}
}
但由于某种原因,我无法显示包含表中的数据?
通常你会做这样的事情:@user.User.Profile.XXXX 但后来我得到错误:System.NullReferenceException: Object reference not set to an instance of an object
所以这意味着返回为空,但数据透视表中有用户具有个人资料。
模型:
Group.cs:
namespace App.Models
{
public class Group : Item
{
public Group() : base()
{
}
[Key]
public Int16 Id { get; set; }
public string Name { get; set; }
public string Images { get; set; }
/* Foreign Keys */
public Nullable<Int16> RegionId { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<Lets> Lets { get; set; }
public virtual ICollection<GroupUser> Users { get; set; }
}
}
应用用户:
namespace App.Models.Identity
{
public class ApplicationUser : IdentityUser
{
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public Nullable<DateTime> UpdatedAt { get; set; }
public Nullable<DateTime> DeletedAt { get; set; }
/* Virtual or Navigation Properties */
public virtual Profile Profile { get; set; }
public virtual ICollection<GroupUser> Groups { get; set; }
public virtual ICollection<Lets> Lets { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Region> Regions { get; set; }
public virtual ICollection<Status> Status { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
}
组用户:
namespace App.Models
{
public class GroupUser
{
public GroupUser()
{
}
public Nullable<Int16> GroupId { get; set; }
public string UserId { get; set; }
public virtual Group Group { get; set; }
public virtual ApplicationUser User { get; set; }
}
}
Profile.cs:
namespace App.Models
{
public class Profile : Item
{
public Profile() : base()
{
}
[Key]
public string UserId { get; set; }
public string FirstName { get; set; }
public string SurName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public Int16 Age { get; set; }
public string City { get; set; }
public string Image { get; set; }
public Int16 Credits { get; set; }
public Int16 Postalcode { get; set; }
}
}
如何使用 razor 显示嵌套数据?
【问题讨论】:
-
你会分享模型吗?我认为那里有问题。
-
我已经更新了问题:)。
-
使用
ToList()代替.AsEnumerable()。 -
私有 IEnumerable
GetUsers(int groupId) { IEnumerable model = null; if(groupId == 0) { model = _kletsContext.GroupUser.Include(g => g.Group).OrderByDescending(o => o.GroupId).ToList(); } else { 模型 = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User.Profile).ToList(); } 返回模型;所以我结束了这个。但这不起作用。我仍然遇到同样的错误。
标签: c# asp.net-mvc entity-framework razor entity-framework-core