Currently in the process of developing an event management system where I need to create events and generate multiple tickets for each event. Utilizing c# and ASP.NET MVC, I have set up the following model classes;
public class Event
{
public int EventID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Location { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public String Description { get; set; }
[Required]
public int TicketsAvailable { get; set; }
//navigation property
public virtual ICollection<Order> Order { get; set; }
//navigation property
public virtual ICollection<Ticket> Ticket { get; set;}
}
public class Ticket
{
public int TicketID { get; set; }
[Required]
[ForeignKey("Event")]
//foreign key
public int EventID { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float Price { get; set; }
//navigation property
public virtual Event Event { get; set; }
//navigation property
public ICollection<OrderDetails> OrderDetails { get; set; }
}
Initially used Scaffolded CRUD views for the events and am currently faced with a challenge on how to pass the EventID for a newly created event to the AddTicket view in order to create event-specific tickets. Below is a snippet from my controller class;
public class Events1Controller : Controller
{
private IEventRepository _eventRepository;
private ITicketRepository _ticketRepository;
public Events1Controller()
{
this._eventRepository = new EventRepository(new ApplicationDbContext());
this._ticketRepository = new TicketRepository(new ApplicationDbContext());
}
// GET: Events
[AllowAnonymous]
public ActionResult Index()
{
return View(_eventRepository.GetEvents());
}
// GET: Events/Create
[AllowAnonymous]
public ActionResult Create()
{
return View();
}
...
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Having incorporated forms within my razor views, successfully saving events into the database with correct EventIDs being passed to tickets displays correctly in the SaveTicket view. However, hitting a roadblock when attempting to save the ticket as it fails and the page simply reloads. Numerous tutorials have been explored without resolving the issue, leaving me stuck for quite some time now.