如何使用viewmodel连接两个表和一个获取列表来查看?

时间:2015-04-08 12:20:06

标签: c# asp.net-mvc-5 viewmodel

我有两个名为" Events"和" EventUser",

 public partial class Event
{
    public Event()
    {
        this.EventUsers = new HashSet<EventUser>();
        this.Objects = new HashSet<Object>();
    }

    public int Id { get; set; }
    public Nullable<int> CreatedBy { get; set; }
    public Nullable<System.DateTime> StartTime { get; set; }
    public Nullable<bool> IsShared { get; set; }
    public Nullable<double> Budget { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<EventUser> EventUsers { get; set; }
    public virtual ICollection<Object> Objects { get; set; }
}

 public partial class EventUser
{
    public Nullable<int> EventId { get; set; }
    public Nullable<int> UserId { get; set; }
    public bool IsAccepted { get; set; }
    public int EUPK { get; set; }

    public virtual Event Event { get; set; }
    public virtual User User { get; set; }
}

当UserId的参数传递给动作方法时,我会计划显示邀请到该UserId的所有事件以及邀请参加该事件的所有其他用户......

例如,参数userId是2;我想显示userId 2&amp;的所有EventIds。对于每个EventId,所有UserIds和其他细节..

我试过这个&amp;我已经获得了有关活动的一些信息,但没有获得每个活动邀请的用户列表..

这是我的行动方法;

public ActionResult Index(int UId)

    {

        ViewBag.NotCount = 1;

        var result1 = from eu in db.EventUsers where eu.UserId == UId select eu;



        var result2 = from eu1 in result1
                      join e in db.Events on eu1.EventId equals e.Id
                      select new MessageViewModel {

                      EventID1 = e.Id,
                      IsAccepted= eu1.IsAccepted,
                      CreatedBy = e.CreatedBy,
                      Budget = e.Budget,


                      };




        return View(result2);
    }

我的观点是;

@{var counter = 1;}

@if (!Model.Any())
{
    <div  style="margin-left:550px;margin-top:200px">
        <h2>"There are no notifications for you"</h2>

    </div>

}

else { 

foreach (var item in Model)
{


        <div class="panel panel-info" style="width:700px;margin-left:400px">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
        <div class=" panel-heading">
            <h3 class="panel-title">Notification @counter</h3>
        </div>
        <div class="panel-body"><p>Event @item.EventID1 is created by @item.CreatedBy .Budget of the event is @item.Budget <p>

@if (item.IsAccepted)
{
<p>You have accept the invitation!!!</p>
}

@if (!item.IsAccepted)
{
    <p>You haven't accept the invitation yet!!!!</p>
}

            @{counter++;}

      </div>
    </div>


}

我的viewmodel是;

  public class MessageViewModel
{
    public int EventID1 { get; set; }
    public bool IsAccepted { get; set; }
    public int? CreatedBy { get; set; }
    public DateTime StartTime { get; set; }

    public bool IsShared { get; set; }
    public int ObjectID { get; set; }
    public double? Budget { get; set; }

    public IEnumerable<EventUser> Euser { get; set; }
   // don't know if this is the
   // right way to get list of userids
}

我怎样才能克服这个问题?如果我的方法错了,还有除此之外的任何方法吗?

1 个答案:

答案 0 :(得分:1)

如果您没有使用延迟加载,则必须使用Include命令手动指定所需的关系。它在您正在使用的类似sql的查询语法中称为“join”。

您在Event和EventUser之间包含了一个连接,但EventUser和User之间需要另一个连接,因为User是一个不同的实体。

我无法帮助您处理代码,因为我们无法判断您的User对象上的主键是什么。但请查看您已经实现的连接语法,并按照相同的格式在EventUser和User之间添加另一个连接。