【发布时间】:2015-12-13 03:24:17
【问题描述】:
在我的 asp.net mvc 网站上,我希望用户能够单击我的事件表中的预订按钮,然后被定向到一个视图,该视图具有可用于该事件的门票列表,并且能够单击另一个按钮将每种类型的票添加到他们的购物篮/订单中。但是我很难实现这一点。我无法从表中获取 EventID(事件的主键),当他们单击索引视图中表中的预订按钮时,将其传递到下一个票务预订视图以检索具有相应 EventID 的票(票的外键)。任何帮助将不胜感激,因为我多年来一直坚持这一点。
这是我的活动和门票模型
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; }
}
这是我的事件索引模型
<table class="pure-table pure-table horizontal">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Location</th>
<th>Description</th>
<th>Book Event</th>
</tr>
</thead>
<tbody>
@foreach (var events in Model)
{
<tr class="even">
<td>@Html.DisplayFor(x => events.Name)</td>
<td>@Html.DisplayFor(x => events.Date)</td>
<td>@Html.DisplayFor(x => events.Location)</td>
<td>@Html.DisplayFor(x => events.Description)</td>
<td>@Html.ActionLink("Book", "Book", new { id = events.EventID })</td>
</tr>
}
</tbody>
</table>
这是我的订票视图
@model Site.Models.Ticket
<head>
<title>Order</title>
</head>
<body>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Description</th>
<th>Price</th>
<th>Add</th>
</tr>
@foreach(Site.Models.Ticket t in ViewBag.listProducts)
{
<tr>
<td>@t.Description</td>
<td>@t.Price</td>
</tr>
}
</table>
</body>
这是我的事件控制器
public class EventsController : Controller{
private EventRepository _eventRepository;
public EventsController()
{
this._eventRepository = new EventRepository();
}
[AllowAnonymous]
public ActionResult Index()
{
//return View(db.Events.ToList());
return View(_eventRepository.GetEvents());
}
// GET: Events/Edit/5
[AllowAnonymous]
public ActionResult Book(int? id)
{
return RedirectToAction("CreateOrder", "Order", new { id = id });
}
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult Book([Bind(Include = "EventID,Name,Location,Date,Description,TicketsAvailable")] Event @event)
{
if (ModelState.IsValid)
{
return RedirectToAction("Book", "Book", new { id = @event.EventID });
}
return View(@event);
}
}
}
还有我的订票控制器
public class OrderController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public OrderController()
{
}
[AllowAnonymous]
public ActionResult CreateOrder(int id)
{
// Ticket ticket = new Ticket();
// ticket.EventID = id;
// ViewBag.EventID = new SelectList(db.Events, "EventID", "Name");
// return View(ticket);
ViewBag.listTickets = (from t in db.Tickets where t.EventID == id select t).ToList();
return View();
}
}
【问题讨论】:
标签: javascript c# asp.net asp.net-mvc