I am trying to create a asp.net mvc site using c#. I have two models an Event
model and a Ticket
model. I want to be able to create an Event
using the supplied CRUD views and then when create is clicked the user should be forwarded to another page where they can add tickets to this event. So far I have got it working so an event can be created and the user can create one ticket. However I need multiple tickets per event so I believe a ViewModel may be useful but I've no idea what it would look like or how to use it to allow multiple tickets to be added in my view.
Here are my current models;
public class Event
{
public int EventID { get; set; }
[Required]
public String Name { get; set; }
[Required]
public String Location { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
[Required]
[DataType(DataType.Time)]
public DateTime StartTime { 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; }
}
Here is my attempt at an appropriate viewmodel
public class EventTicketViewModel
{
public Event events {get; set;}
public List<Ticket> tickets { get; set; }
}
Here is my create Ticket view
@model GeogSocSite.Models.Ticket
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Ticket</h4>
<hr />
@Html.HiddenFor(model => model.EventID)
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
And finally here is my TicketController class
public class Tickets1Controller : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
private TicketRepository _ticketRepository;
public Tickets1Controller()
{
this._ticketRepository = new TicketRepository();
}
// GET: Tickets
[AllowAnonymous]
public ActionResult Index()
{
return View(_ticketRepository.GetTickets());
}
// GET: Tickets1/Details/5
[AllowAnonymous]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ticket ticket = _ticketRepository.GetByID(id);
if (ticket == null)
{
return HttpNotFound();
}
return View(ticket);
}
// GET: Tickets1/Create
[AllowAnonymous]
public ActionResult Create(int id)
{
Ticket ticket = new Ticket();
ticket.EventID = id;
return View(ticket);
}
// POST: Tickets1/Create
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult Create([Bind(Include = "TicketID,EventID,Description,Price")] Ticket ticket)
{
if (ModelState.IsValid)
{
_ticketRepository.InsertTicket(ticket);
_ticketRepository.Save();
return RedirectToAction("Index");
}
return View(ticket);
}
// GET: Tickets1/Edit/5
[AllowAnonymous]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ticket ticket = _ticketRepository.GetByID(id);
if (ticket == null)
{
return HttpNotFound();
}
return View(ticket);
}
// POST: Tickets1/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult Edit([Bind(Include = "TicketID,EventID,Description,Price")] Ticket ticket)
{
if (ModelState.IsValid)
{
_ticketRepository.UpdateTicket(ticket);
return RedirectToAction("Index");
}
return View(ticket);
}
// GET: Tickets1/Delete/5
[AllowAnonymous]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ticket ticket = _ticketRepository.GetByID(id);
if (ticket == null)
{
return HttpNotFound();
}
return View(ticket);
}
// POST: Tickets1/Delete/5
[HttpPost, ActionName("Delete")]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
_ticketRepository.DeleteTicket(id);
_ticketRepository.Save();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
If anyone can help me with how to either create the event and add multiple tickets to the event all on one page or how to save the event then create multiple tickets I'd be really grateful as I've been stuck on this for ages now.