在视图中显示视图模型
提问开始:
因此,我有一个名为Reports的视图模型类,如下所示:
public class MemberReports
{
public Member Members { get; set; }
public Event Events { get; set; }
public MemberClubHours Memberclubhours { get; set; }
public VolunteerEvents Volunteerevents { get; set; }
}
它包含成员,与事件、MemberClubHours和志愿者事件具有多对多关系。基本上,我希望在一个名为reports的视图中显示成员的所有数据。但是我很难做到这一点,有什么建议吗?下面是所有的类:
public class Member
{
public int Id { get; set; }
[Required]
[MaxLength(4, ErrorMessage = "PIN must be 4 numbers long"), MinLength(4, ErrorMessage = "PIN must be 4 numbers long")]
public string PIN { get; set; }
[Required]
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Date of Birth")]
public DateTime? Birthdate { get; set; }
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<VolunteerEvents> Vevents { get; set; }
public virtual ICollection<MemberClubHours> Hours { get; } = new HashSet<MemberClubHours>();
}
public class Event
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Display(Name = "Date")]
public DateTime? EventDate { get; set; }
[Required]
[Display(Name = "Start Time")]
public TimeSpan EventStartTime { get; set; }
[Required]
[Display(Name = "End Time")]
public TimeSpan EventEndTime { get; set; }
public int EventTotalTime{ get; set; }
public virtual ICollection<Member> Members { get; set; }
}
public class MemberClubHours
{
public int Id { get; set; }
public DateTime? ClockIn { get; set; }
public DateTime? ClockOut { get; set; }
[Required]
public Member Member { get; set; }
}
public class VolunteerEvents
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Display( Name ="Date")]
public DateTime? VEventDate { get; set; }
[Required]
[Display(Name = "Start Time")]
public TimeSpan VEventStartTime { get; set; }
[Required]
[Display(Name = "End Time")]
public TimeSpan VEventEndTime { get; set; }
public int VEventTotalTime { get; set; }
public virtual ICollection<Member> Members { get; set; }
}
这是控制器,我有点困惑,我是不是应该遍历这些类来获得要存储在视图模型中的信息?
public ActionResult Reports()//reports page
{
var Data = new MemberReports()
{
};
return View(Data);
}
最后是这里的视图:
@model IEnumerable<ClubChallenge.ViewModels.MemberReports>
@{
ViewBag.Title = "Reports";
}
<table>
@foreach (var data in Model)
{
<tr>
<td></td>
</tr>
}
</table>
回答开始:得票数 1 在所有这些导航属性中,您都分配了virtual
关键字。实体框架中的virtual
关键字表示此相关实体将被延迟加载或自动加载,而无需查询。
public class Member
{
...
public virtual ICollection<Event> Events { get; set; }
public virtual ICollection<VolunteerEvents> Vevents { get; set; }
public virtual ICollection<MemberClubHours> Hours { get; } = new HashSet<MemberClubHours>();
}
毕竟,您可能不需要MemberReports,因为导航属性是自动加载的,您只需搜索一个成员并将其返回到视图即可。试试这个;
public ActionResult Reports()
{
// If your navigation properties doesn't have the virtual keyword, you need to write your query like this;
// var member = context.Members.Include(m=>m.Events).Include(m=>m.Vevents).Include(m=>m.Hours).ToList();
// but since, you put virtual, you could just do this;
// all the other navigation properties are automatically loaded
var members = context.Members.ToList();
// you could immediately access them like this
// get the first member
Console.WriteLine(members.FirstOrDefault().Events.Count);
Console.WriteLine(members.FirstOrDefault().Events.FirstOrDefault().Name);
Console.WriteLine(members.FirstOrDefault().Vevents.Count);
Console.WriteLine(members.FirstOrDefault().Vevents.FirstOrDefault().Name);
Console.WriteLine(members.FirstOrDefault().Hours.Count);
Console.WriteLine(members.FirstOrDefault().Hours.ClockIn.ToString());
return View(member);
}
然后在您的视图中,您可以执行嵌套循环。首先循环通过成员,然后在内部循环通过导航的属性;
@model IEnumerable<ClubChallenge.Members>
@{
ViewBag.Title = "Reports";
}
<table>
@foreach (var data in Model)
{
<tr>
<td>
@foreach(var event in data.Events){
<text>@event.Name ,</text>
}
</td>
<td>
@foreach(var vevent in data.Vevents){
<text>@vevent.Name ,</text>
}
</td>
<td>
@foreach(var hour in data.Hours){
<text>@hour.ClockIn.ToString() ,</text>
}
</td>
</tr>
}
</table>
总结 以上是真正的电脑专家为你收集整理的在视图中显示视图模型的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。