HTML Action Link asp.net mvc

时间:2016-03-15 20:41:26

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

我无法通过操作链接传递我的id参数。我想在我的视图中添加一个取消按钮,它会将eventID返回给方法,对数据库执行某些操作,然后重定向到另一个视图。我已进入调试模式,当它返回到方法时,id参数为null,我不明白为什么。

这是我的控制器方法来创建初始视图

public ActionResult Create(OrderVM model)
{

    //get the currentUser ID to search database for user
    string currentUserId = User.Identity.GetUserId();
    int quantity = 10;
     ApplicationUser currentUser = db.Users.Find(currentUserId);

     Order order = new Order
     {
            OrderDate = DateTime.UtcNow,
            EventID = model.EventID,
            Event = currentEvent,
            user = currentUser

       };
                //add this order to the database
                db.Orders.Add(order);
                //save changes to database
                db.SaveChanges();

                SummaryVm summaryVm = new SummaryVm
                {
                    email = order.user.Email,
                    orderID = order.OrderID,
                    tickets = model.Tickets,
                    totalPrice = total,
                    ticketQuantity = quantity,
                    orderDate = order.OrderDate,
                    eventID = model.EventID,

                };

                return View("OrderSummary", summaryVm);
            }

这是OrderSummary视图

@model Site.ViewModels.SummaryVm
<head>
<title>Next Page</title>

</head>


    <header>
        <h2> Here is your order summary, please review before proceeding with payment. Please complete this transaction within 15 minutes or your order  will be cancelled. </h2>
    </header>
        <div class="float-left">
             <h2 id="countdown"></h2>
        </div>
    <div id="content">

        <div class="panel panel-default">
            <div class="panel-heading">Order Summary</div>
            <div class="panel-body">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Ticket Type</th>
                            <th><span contenteditable>Price</span></th>
                            <th><span contenteditable>Quantity</span></th>

                        </tr>
                    </thead>
                    <tbody>
                        @foreach (GeoSocietySite.ViewModels.TicketVm ticket in @Model.tickets)
                        {
                            <tr>
                                <td><span contenteditable>@ticket.Description</span></td>
                                <td><span contenteditable>@ticket.Price</span></td>
                                <td><span  contenteditable>@ticket.Quantity</span></td>
                            </tr>
                        }
                    </tbody>
                </table>

                <table id="total-table">
                    <tr>
                        <th>Total Price in £: </th>
                        <td id="custom-amount">@Model.totalPrice</td>
                    </tr>
                </table>
            </div>
            </div>
        </div>
         @Html.ActionLink("Cancel", "CancelOrder", "Order", new { id = @Model.eventID })

这是我的CancelOrder控制器方法

    public ActionResult CancelOrder(int eventID)
    {
        //do something to db
        return RedirectToAction("CreateOrder", "Order", new { id = eventID    });
    }

这是我的创建订单方法

public ActionResult CreateOrder(int? id)
    {
        Event currentEvent = db.Events.Find(id);

        //check event exists
        if (currentEvent != null)
       {
         //get the ID of the user currently logged in to the site
         string currentUserId = User.Identity.GetUserId();
         //retreive details of current user from db based on ID
         ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);

          // Initialize a new order
          OrderVM model = new OrderVM
          {
             EventID = currentEvent.EventID,
             EventName = currentEvent.Name,
             OrderDate = DateTime.Now,
             FirstName = currentUser.FirstName,
             EventDate = currentEvent.Date,
             EventDescription = currentEvent.Description,
             EventLocation = currentEvent.Location,
            //create view model for each ticket type then place in list 
             Tickets = currentEvent.Tickets.Select(t => new TicketVm
             {
                    TicketID = t.TicketID,
                    Description = t.Description,
                    Price = t.Price
                    }).ToList(),
                };

                return View(model);
            }else
            {
              return RedirectToAction("Index", "Events");
        }
    }

当我尝试运行此代码时,我将重定向到“事件索引”页面,这意味着尚未从数据库中检索到事件,并且通过调试我可以看到这是因为ID参数为null。我已经检查过,当summaryVm传递给OrderSummary视图时,它完全填充了数据。如果有人能看到我做错了什么,任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:3)

您使用了错误的info重叠(以及您添加的ActionLink,而不是路由值)。此外,方法中的参数名为htmlAttributes,而不是eventID。将其更改为

id