在Views之间传递参数以显示信息

时间:2015-12-12 16:40:52

标签: javascript c# asp.net asp.net-mvc

在我的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();
               }
}

1 个答案:

答案 0 :(得分:0)

除了您的预订视图外,您似乎正在遵循正确的方法:

您没有从action方法传递视图模型。所以你不需要@model Site.Models.Ticket声明

在这里,您应该关注ViewBag.listTickets而不是ViewBag.listProducts

@foreach(Site.Models.Ticket t in ViewBag.listTickets)

希望这有帮助!