2

I am having trouble passing my id parameter through an action link. I want to add a cancel button to my view which will return the eventID to the method, do something to the database, then redirect to another view. I have entered debug mode and the id param is null when it is returned to the method and I don't understand why.

Here is my controller method to create the initial view

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);
            }

This is the OrderSummary view

@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 })

This is my CancelOrder controller method

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

This is my create order method

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");
        }
    }

When I try to run this code I am redirect to the Events Index page, meaning the Event has not been retrieved from the database and through debugging I can see this is because the ID parameter is null. I have checked that when the summaryVm is passed to the OrderSummary view it is fully populated with data.If anyone can see what I am doing wrong any help would be greatly appreciated. Thank you!

1 Answer 1

3

Your using the wrong overload of ActionLink (and your adding htmlAttributes, not route values). In addition, the parameter in your method is named eventID, not id. Change it to

@Html.ActionLink("Cancel", "CancelOrder", "Order", new { eventID = Model.eventID }, null)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.