Before I start, I'm relatively new to coding and was due to start my first junior role in the upcoming future, so my current skill level is pretty basic.
I'm creating a personal C# ASP.NET MVC web application to track music events that users go to, and the artists that are playing at those events, and which of the artists a user sees. This is all done by the user manually adding events from an Indexed view of all events within the database (SQL Server) which is show in the image linked below.
https://i.stack.imgur.com/HOUGG.png
The controller action:
public ActionResult GetEvents()
{
return View(_trackerService.GetEvents());
}
The markup for the view:
@model IEnumerable<Tracker.Data.tbl_events>
@{
ViewBag.Title = "GetEvents";
}
<h2>GetEvents</h2>
<p>
@Html.ActionLink("Create New", "CreateEvent")
</p>
<table class="table">
<tr><h2>Upcoming Events</h2>
<th>
@Html.DisplayNameFor(model => model.Event_ID)
</th><th>
@Html.DisplayNameFor(model => model.Event_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Event_Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Event_Location)
</th>
</tr>
@foreach (var item in Model.Where( x => x.Event_Date >= System.DateTime.Now).OrderBy(x => x.Event_Date))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Event_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Event_Name)
</td>
<td>
@Convert.ToString(string.Format("{0:dd/MM/yyyy}", item.Event_Date)) @*Converts the DateTime data type that ASP.NET uses by default into a string with the format of Date Only (https://stackoverflow.com/a/34990313/12764653)*@
</td>
<td>
@Html.DisplayFor(modelItem => item.Event_Location)
</td>
<td>
@*@Html.ActionLink("Edit", "Edit", new { id = item.Event_ID }) | *@
@Html.ActionLink("Details", "GetEventDetails", new { Event_ID = item.Event_ID }) |
@Html.ActionLink("Lineup", "../Event/GetLineup", new { Event_ID = item.Event_ID, Event_Name = item.Event_Name }) |
@if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{@*Checks to see if there is a current user*@
@Html.ActionLink("Add to my Events", "../Event/AddToUser", new { User_ID = Convert.ToString(System.Web.HttpContext.Current.User.Identity.GetUserId()).GetHashCode(), Event_ID = item.Event_ID })}
@*@Html.ActionLink("Delete", "Delete", new { id = item.Event_ID })*@
</td>
</tr>
}
</table>
Once a user adds an event to their profile, it creates an entry into the database with the fields (Linked Below):
https://i.stack.imgur.com/YQqHT.png
The controller method for the 'Add To My Events' function:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult AddToUser(tbl_eventhistory _event)
{
try
{
_trackerService.AddToUser(_event);
return RedirectToAction("GetEvents");
}
catch
{
return View();
}
}
I understand that with the way it is currently, this would most likely have to be done either in the controller or View, and it would be done to check 'tbl_eventhistory' to see if an entry exists with the current users User_ID and a specific Event_ID, however I'm unsure of how to actually do this so any help would be greatly appreciated.
EDIT: The table shown is an indexed view of 'tbl_events'. When a user adds an event to their profile from this view, it creates an entry in a different table called 'tbl_eventhistory' using the parameters for that specific event, which is related through a foreign key on the Event_ID (tbl_event's PK). When an event is added to tbl_eventhistory by the user, I want to remove the link 'Add To User' from the view for that specific event only.