gpt4 book ai didi

c# - 将某人添加到多对多的事件中

转载 作者:行者123 更新时间:2023-11-30 17:30:28 24 4
gpt4 key购买 nike

有一个约会/仪式应用程序,我希望志愿者能够在其中注册一个仪式。他们必须登录才能这样做。

一个仪式可以有很多志愿者,一个志愿者可以有很多仪式

我正在尝试在 FullCalendar 中执行此操作。我想让志愿者点击一个仪式,然后点击一个按钮加入事件。

我很难理解如何将志愿者添加到仪式中。我知道我需要添加到 Ceremony 实体中的列表。

这是我正在处理的 Controller :

    [HttpPost]
public JsonResult AddVolunteerToCeremony(Volunteer v, Appointments a)
{
string username = Membership.GetUser().UserName;

var getVolunteer = (from vol in db.Volunteers
where username == vol.Username
select vol).SingleOrDefault();

v.Appointments = new List<Appointments>();

var status = false;
using (ChurchDBContext db = new ChurchDBContext())
{
if (a.AppointmentId > 0)
{
//Update the event
var getCeremonies = db.Appointments.Where(d => d.AppointmentId == a.AppointmentId).FirstOrDefault();
if (v != null)
{
v.Appointments.Add(getVolunteer);

}
}

db.SaveChanges();
status = true;
}
return new JsonResult { Data = new { status = status } };
}

实体:

public class Appointments
{
[Key]
public int AppointmentId { get; set; }

public string DetailsOfAppointment { get; set; }

public int? Fee { get; set; }

public string RoomType { get; set; }

public string NameOfApplicant { get; set; }

public string ApplicantPhoneNumber { get; set; }

public string ApplicantEmail { get; set; }

[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
public DateTime DateOfAppointment { get; set; }

public string ThemeColour { get; set; }

public Boolean Confirmed { get; set; }

[ForeignKey("Admins")]
public int AdministrationId { get; set; }

[ForeignKey("Church")]
public int ChurchId { get; set; }

//[ForeignKey("Volunteers")]
//public int VolunteerId { get; set; }

public virtual Church Church { get; set; }

public virtual Administration Admins { get; set; }

public ICollection <Volunteer> Volunteers { get; set; }

public Appointments()
{
this.Volunteers = new HashSet<Volunteer>();
}
}

public class Volunteer
{
[Key]
public int VolunteerId { get; set; }

public string Name { get; set; }

public string Username { get; set; }

public string Email { get; set; }

public bool GardaVetted { get; set; }

public string VolunteerRole { get; set; }

public string VolunteerPhoneNumber { get; set; }

[ForeignKey("Church")]
public int ChurchId { get; set; }

//Foreign Key
public virtual Church Church { get; set; }

public virtual ICollection<Appointments> Appointments { get; set; }

//public virtual Appointments app { get; set; }

public Volunteer()
{
this.Appointments = new HashSet<Appointments>();
}

}

我是喜欢它还是完全不喜欢它?

最佳答案

看起来您只是更新了 UI 数据而不是数据库。

...
if (a.AppointmentId > 0)
{
//Update the event
var getCeremonies = db.Appointments.Where(d => d.AppointmentId == a.AppointmentId).FirstOrDefault();
if (v != null && getCeremonies != null)
{
v.Appointments.Add(getVolunteer);
getCeremonies.Volunteers.Add(getVolunteer);
}
}

db.SaveChanges();

关于c# - 将某人添加到多对多的事件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49758404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com