I have models Event and Ticket
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.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; }
}
And I have created two View Models to allow me to add multiple Tickets to each Event
public class TicketVm
{
[Required]
public string Description { get; set; }
[Required]
public float Price { get; set; }
}
public class CreateTicketVm
{
public int EventId { get; set; }
public List<TicketVm> Tickets { set; get; }
}
Then I have a View which allows a User to create a new Event. Then when they submit this event they are directed to the CreateTicket View which is supposed to add new tickets to this Event using the same EventID as the event created.
This is my Create Ticket view
@model Site.Models.CreateTicketVm
<head>
<title>Create Event Tickets</title>
<link href="Content/TicketStyles.css" rel="stylesheet" type="text/css">
</head>
<h3> Tickets</h3>
<div id="tickets"></div>
@Html.HiddenFor(s => s.EventId)
<input type="button" id="add" value="Add Ticket" />
<input type="submit" id="btnSave" />
@section scripts{
<script>
$(function () {
//Lets create the first row
addNewRow();
$("#add").click(function (e) {
e.preventDefault();
addNewRow();
});
$("#btnSave").click(function (e) {
e.preventDefault();
var createTicket = { EventId: $("#EventId").val(), Tickets: [] };
var rows = $("#tickets").find(".item");
$.each(rows, function (i, row) {
var _row = $(this);
var inputs = _row.find("input");
var ticket = {};
$.each(inputs, function (index, item) {
var _this = $(this);
ticket[_this.data("for")] = _this.val();
});
createTicket.Tickets.push(ticket);
});
var targetUrl = "@Url.Action("CreateTicket","Tickets1")";
$.ajax({
url: targetUrl,
contentType: "application/json",
data: JSON.stringify(createTicket),
type: "POST"
}).done(function (res) {
if (res.Status === "Success") {
window.location.href = "@Url.Action("Success")";
}
});
});
});
function addNewRow() {
$.get("@Url.Action("AddNewTicketRow","Tickets1")", function (res) {
$("#tickets").append(res);
});
}
</script>
}
This is my AddNewTicket partial view
@model Site.Models.TicketVm
<div class="item">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div>
@Html.TextBoxFor(model => model.Description, new { @data_for = "Description" })
</div>
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div>
@Html.TextBoxFor(s => s.Price, new { @data_for = "Price" })
</div>
</div>
And this is my Tickets1Controller class
public class Tickets1Controller : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult CreateTicket(int id)
{
var vm = new CreateTicketVm { EventId = id };
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateTicket(CreateTicketVm model)
{
if (model != null)
{
foreach (var ticketVm in model.Tickets)
{
db.Tickets.Add(new Ticket{ EventID = model.EventId,
Description=ticketVm.Description, Price =ticketVm.Price});
db.SaveChanges();
}
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
public ActionResult AddNewTicketRow()
{
var vm = new TicketVm();
return PartialView("~/Views/Tickets1/Partials/AddNewTicketRow.cshtml", vm);
}
My Tickets are not saving and I do not understand why because the EventID is being passed to the CreateTicket view but nothing happens when the Submit button is clicked and the tickets are not being saved to my database table. Any suggestions about how to fix this would be really appreciated thanks.