ฉันมีสองตารางชื่อ "กิจกรรม" และ "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; ฉันต้องการแสดง EventIds ทั้งหมดของ userId 2 & สำหรับแต่ละ EventId UserIds ทั้งหมดและรายละเอียดอื่น ๆ
ฉันลองสิ่งนี้ & ฉันได้รับข้อมูลบางอย่างเกี่ยวกับกิจกรรม แต่ไม่ใช่รายชื่อผู้ใช้ที่ได้เชิญสำหรับแต่ละกิจกรรมด้วย
นี่คือวิธีดำเนินการของฉัน
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="/th#" class="close" data-dismiss="alert">×</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>
}
และโมเดลมุมมองของฉันคือ;
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
}
ฉันจะเอาชนะปัญหานี้ได้อย่างไร มีวิธีอื่นนอกเหนือจากนี้หรือไม่หากแนวทางของฉันผิด