1

I have 3 models:

  1. Participant
  2. Event
  3. Enrollment

Each of them have their own tables in database. The target is to be able seeing Event model data, Enrollment model data and creating Participant on one page.

Enrollment shows who of Participants are assigned to the Event.

Question: how to assign Participant to the Event so I could use Enrollment?

P. S. If you would need additional information, tell me please. And sorry for inconvenience...

public class Event
{
    public int ID { get; set; }
    public string Title { get; set; }   
    public DateTime Date { get; set; }
    public string Location { get; set; }
    public string Comment { get; set; }

    public ICollection<Enrollment> Enrollments { get; set; }
    //public ICollection<EventAssignment> EventAssignments { get; set; }

}

public class Participant
{
    public int ID { get; set; }      
    public string FirstName { get; set; }        
    public string LastName { get; set; }        
    public string PersonalCode { get; set; }      
    public string PaymentType { get; set; }       
    public string Comment { get; set; }

    public ICollection<Enrollment> Enrollments { get; set; }
}

public class Enrollment
{
    public int ID { get; set; }
    public int EventID { get; set; }
    public int ParticipantID { get; set; }

    public Participant Participant { get; set; }
    public Event Event { get; set; }
}

    //Creating Participant
    public async Task<IActionResult> OnPostAsync()
    {

        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Participants.Add(Participant);
        await _context.SaveChangesAsync();

        return RedirectToPage("/Participants/Index");
    }
desmondische
  • 186
  • 13
  • Do you have a working form where you enter data you want to save to the database? – TheMixy Mar 15 '21 at 06:30
  • 1
    According to your code, the Participant and Event entity contains many to many relationships and the Enrollment entity is the join entity (join table). Do you mean, when you create one Participant, in the Create page, you want to add multiple Events for the current Participant? If that is the case, in the Create page model, you could add the following properties:`[BindProperty] public Participant Participant { get; set; } [BindProperty] public List Events { get; set; }`, then, using them to display data in the view, or get entered value from them and save into database. – Zhi Lv Mar 15 '21 at 07:24
  • 1
    In the Post method, you could loop through the Events, then, create the Enrollment using the existing participant and event (it can add relationship for them), after that, insert data into database. Refer [Saving many-to-many relationship in Entity Framework Core](https://stackoverflow.com/questions/38893873/saving-many-to-many-relationship-in-entity-framework-core) – Zhi Lv Mar 15 '21 at 07:46
  • @Zhi Lv , Actually I am adding a Participant through Events/Details.cshtml.cs (I just added OnPost method to it that saves Participant). And I need not to add Event to Participant but Participant to Event. So the ending view should be like this: for example, /Events/Details?=1 where on the top you can see the Details (read data from Events table) of the Event, then goes data about enrolled/assigned Participants to the Event (using Mode.Event.Enrollment.Participant.{property}), and the goes a POST Form for creating Participant that automatically assigns/enrolls to the particular Event. – desmondische Mar 15 '21 at 07:53
  • Hope you got me! And Yes, I tried to find the solution using those docs. My models are like theirs Student-Enrollment-Course. But the problem is that they don’t assign any courses to student, they do it by seeding db. This way works perfectly but it is not what I need. – desmondische Mar 15 '21 at 07:53
  • @ZhiLv Still struggling. I am pretty much sure I miss something really obvious. Haha – desmondische Mar 15 '21 at 08:53

1 Answers1

0

Wow. How easy it was :D

public async Task<IActionResult> OnPostAsync(int id)
    {

        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Participants.Add(Participant);
        await _context.SaveChangesAsync();

        // Just creating new Enrollment depending on page id for eventID
        // and  getting just created Participant.ID as ParticipantID
        var enr = new Enrollment()
        {
            EventID = id,
            ParticipantID = Participant.ID
        };

        _context.Enrollments.Add(enr);
        await _context.SaveChangesAsync();


        return RedirectToPage("/Participants/Index");
    }
desmondische
  • 186
  • 13