我在创建合适的ViewModel时遇到问题,允许我允许用户在视图中一次选择多张故障单。
我有一个Ticket模型
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; }
}
订单模型
public class Order
{
public int OrderID { get; set; }
//foreign key
[Required]
[ForeignKey("Event")]
public int EventID { get; set; }
[Required]
public DateTime OrderDate { get; set; }
[Required]
public float OrderTotal { get; set; }
public ApplicationUser user { get; set; }
public virtual Event Event { get; set; }
}
然后我有一个OrderDetails模型
public class OrderDetails
{
[Required]
public int OrderDetailsID { get; set; }
[Required]
//Foreign key
[ForeignKey("Order")]
public int OrderID { get; set; }
[Required]
//Foreign key
[ForeignKey("Ticket")]
public int TicketID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
public int PhoneNumber { get; set; }
//navigation properties
public virtual Order Order { get; set; }
public virtual Ticket Ticket { get; set; }
}
我还创建了一个额外的TicketsOrdered模型,因为我不知道在哪里我可以保存用户选择的每种票类型的数量
public class TicketsOrdered
{
public int OrderID;
public int TicketID;
public int Quantity;
}
我想创建一个View,它将显示与特定事件相关的每个故障单的故障单描述和价格,然后还会有一个下拉/文本框,允许用户输入他们需要的每个故障单的数量。然后我想将TicketID,OrderID和数量保存到我的TicketsOrdered数据库表。我遇到的问题是所有数据都与很多模型有关,我不知道如何将它们整合在一起。我完全失去了如何实现这一点,我想要它看起来的一个例子就是你在EventBrite上点击预订票。 ViewModel上的任何帮助都可以使用或者如何在View中使用我当前的模型将非常有用,谢谢。
修改 事件模型
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]
[Display(Name = "Total Tickets Available")]
public int TicketsAvailable { get; set; }
//navigation property
public virtual IEnumerable<Order> Order { get; set; }
//navigation property
public virtual IEnumerable<Ticket> Tickets { get; set; }
}
答案 0 :(得分:2)
首先创建代表您想在视图中显示/编辑的视图模型
public class TicketVM
{
public int TicketID { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
public class OrderVM
{
public int ID { get; set; }
public int EventID { get; set; }
public string EventName { get; set; }
// additional properties of Event you want to display
public DateTime OrderDate { get; set; }
public string FirstName { get; set; }
// additional properties of Order and OrderDetails you want to edit
public List<TicketVM> Tickets { get; set; }
}
附注:有点不清楚为什么你有Order
和OrderDetails
的单独模型,或为什么OrderDetails
有public int TicketID { get; set; }
表明Order
可以只有一张票,所以我建议您可能需要对数据库结构进行一些更改。如果用户可以为不同的人订购多张票,那么每个票持有者的姓名,电子邮件和电话应该存储在TicketsOrdered
表中(以及添加到TicketVM
的那些属性类)
然后视图(假设它为Create方法)
@model TicketVM
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.EventID)
<h2>@Model.EventName</h2>
// other display properties of event
@Html.TextBoxFor(m => m.OrderDate)
@Html.TextBoxFor(m => m.FirstName)
// other edit control for properties of Order and OrderDetails (see note above)
@Html.EditorFor(m => m.Tickets)
<input type="submit" value="Save" />
}
和EditorTemplate
TicketVM
(/Views/Shared/EditorTemplates/TicketVM.cshtml
)
@model TicketVM
@Html.HiddenFor(m => m.TicketID)
@Html.DisplayFor(m => m.Description)
@Html.DisplayFor(m => m.Price)
@Html.TextBoxFor(m => m.Quantity)
然后是控制器方法(注意你没有向你展示Event
模型,所以调整以下内容以适应)
public ActionResult Create(int ID) // assumes ID is the ID of the Event
{
// Get the event and its tickets
Event e = db.Events.Where(e = e.EventID = ID).Include(e => e.Tickets);
// Initialize a new order
OrderVM model = new OrderVM
{
EventID = e.EventID,
EventName = e.EventName,
// other properties of event as required
Tickets = e.Tickets.Select(t => new TicketVM
{
TicketID = t.TicketID,
Description = t.Description,
Price = t.Price
})
};
return View(model);
}
[HttpPost]
public ActionResult Create(OrderVM model)
{
if (!ModelState.IsValid)
{
// repopulate any properties as required
return View(model);
}
// Initialize an Order data model, save it and gets its ID
Order order = new Order
{
OrderDate = model.OrderDate,
// other properties of Order
};
db.Orders.Add(order);
db.SaveChanges();
// ditto for OrderDetails (but see notes above)
// Initialize an TicketsOrdered data model for each valid ticket
foreach(TicketVM ticket in model.Tickets.Where(t => t.Quantity > 0))
{
TicketsOrdered ticketOrder = new TicketsOrdered
{
OrderID = order.ID,
TicketID = ticketOrder.TicketID,
Quantity = ticketOrder.Quantity
}
db.TicketsOrdered.Add(ticketOrder);
}
db.SaveChanges();
return RedirectToAction(...);
}