My application is meant to allow users to add upcoming events from a list to a personal calendar. The personal calendar is implemented as a single table, shared across all users, that links a string UserID value to an int EventID, with a separate int UserCalendarID for a primary key.
However, the table is not updating when the action is called; the action is posted below:
[HttpPost]
public IActionResult AddToCalendar(int ID)
{
var events = context.Events.ToList();
Event _e = events.FirstOrDefault(x => x.EventID == ID);
var calendars = context.Calendars.ToList();
UserCalendar c = new UserCalendar
{
UserID = userManager.GetUserAsync(HttpContext.User).Result.UserName,
EventID = _e.EventID
};
calendars.Add(c);
context.SaveChanges();
return View("Index");
}
For reference, this is the (working) code to add an Event to the Events table:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> NewEvent(Models.NewEventViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var newEvent = new Event {
Artist = await userManager.GetUserAsync(HttpContext.User),
EventName = model.EventName,
Time = model.Time,
Venue = model.Venue,
GenreID = model.GenreID
};
var events = context.Events;
events.Add(newEvent);
context.SaveChanges();
ViewBag.Notification = "Your event has been added.";
ViewBag.Color = "Green";
}
else {
ViewBag.Notification = "Something went wrong — Event not added.";
ViewBag.Color = "Red";
}
return View("Index");
}
The reason I'm not using a ViewModel for AddToCalendar is that there's no view specifically for it; rather, it's triggered by a button in a form on the EventDetails page, which is defined as follows:
@model Event
@{
ViewData["Title"] = "Event Details";
Event e = ViewBag.Event;
}
<h2>@ViewData["Title"]</h2>
<p><b>Event Name: </b>@e.EventName</p>
<p><b>Artist: </b>@e.Artist.ClientName</p>
<p><b>Date and Time: </b>@e.Time</p>
<p><b>Genre: </b>@e.Genre.GenreName</p>
<p><b>Venue: </b>@e.Venue</p>
@if (ViewBag.isYours)
{
<a asp-action="DeleteEvent" asp-controller="Home" class="btn btn-danger" [email protected]>DELETE</a>
<a asp-action="EditEvent" asp-controller="Home" class="btn btn-success" [email protected]>EDIT</a>
}
<form asp-action="AddToCalendar" asp-controller="Home" [email protected] asp-route-returnurl="@ViewData["ReturnUrl"]">
<input type="submit" value="Add To Your Calendar" class="btn btn-default" />
</form>
I have already confirmed that c is being added to calendars. Why does the AddToCalendar event not cause the table to update?