创建包含整数的SelectList

时间:2016-01-06 11:54:44

标签: jquery html asp.net asp.net-mvc c#-4.0

我正在尝试创建一个下拉列表,允许用户选择门票数量。我有一个createOrder方法,根据所选事件显示可用的票证。我想首先检查有多少票可用于该事件,具体取决于可用数量填充我的选择列表

到目前为止,这是我的代码

 public ActionResult CreateOrder(int id)
    {
        Event e1 = db.Events.Find(id);
        List<SelectListItem> quantities = new List<SelectListItem>();
        ViewBag.listTickets = (from t in db.Tickets where t.EventID == id select t).ToList();
        if (e1.TicketsAvailable == 0)
        {
            return View("Error");
        }
        else if (e1.TicketsAvailable < 5)
        {
            for (int loop = 0; loop < e1.TicketsAvailable; loop++)
            {
                quantities.Add(new SelectListItem { Text = loop.ToString(), Value = loop.ToString() });
            }
        }
        else
        {
            for (int loop = 0; loop <= 5; loop++)
            {
                quantities.Add(new SelectListItem { Text = loop.ToString(), Value = loop.ToString() });
            }
        }
            ViewBag.Quantities = quantities;
            return View();

        }

我的观点

<head>
<title>Order</title>
<link rel="stylesheet" href="~/Content/TableSheet.css">
@using GeogSocSite.Models
</head>
<body>
<h1>Choose Your Tickets</h1>
<table align="center" cellspacing="2" border="1">
   <tr>
       <th align="center">Description</th>
       <th align="center">Price</th>
       <th align="center">Add</th>
   </tr>
    @foreach(GeogSocSite.Models.Ticket t in ViewBag.listTickets)
   {
   <tr>
       <td align="center">@t.Description</td>
       <td align="center">@t.Price</td>
       <td align="center">@Html.ActionLink("Add to Cart", "AddToCart", "Order", new { id = t.TicketID })</td>
      <td align="center"> @Html.DropDownList("Quantity", new SelectList(ViewBag.Quantities)) </td>
   </tr>
   }
</table>
<div>
    @Html.ActionLink("Back to List", "Index","Events")
</div>

</body>

但是这不起作用下拉列表中显示的文字是错误的。如果我使用普通列表,这可行,但我认为这意味着用户无法从这些值中进行选择。如果有人能帮助我,我会非常感激,因为我已经被困了很长时间了!

0 个答案:

没有答案