我的模特是这样的:
现在,当用户属于该组时,我想在详细信息页面上显示配置文件中的数据。我是这样做的:
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; }
}
}
ApplicationUser:
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; }
}
}
GroupUser:
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显示嵌套数据?
答案 0 :(得分:1)
model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId)
.Include(gu => gu.User)
.ThenInclude(u => u.Profile)
.OrderByDescending(o => o.GroupId)
.AsEnumerable();
当智能感知不适用于ThenInclude
时,不要被吓到,只需键入它,它就会编译。
答案 1 :(得分:0)
尝试包含用户参考
model = _kletsContext.GroupUser.Where(g =&gt; g.GroupId == groupId) .Include(p =&gt; p.User) .Include(p =&gt; p.User .Profile).OrderByDescending(o =&gt; o.GroupId).AsEnumerable();