mara19
mara19

Reputation: 121

ASP.NET MVC C# set model property values

I am having some problems with setting the values for my summaryVm in the controller method. I am accepting an OrderVM and I have checked in debug mode and the properties of OrderVM are all set when it is passed and the Order also had all values set (including orderID). However later in the method when I try to set the values of my SummaryVm to these values the summaryVm.Tickets is null, as is the summaryVm.orderID. Yet email and eventID are being set fine.

If anyone has suggestions on what I'm doing wrong it would be great because I'm at a loss to see what's wrong.

[HttpPost]
public ActionResult Create(OrderVM model)
{
    if (!ModelState.IsValid)

        return View("CreateOrder", model);
    }

    string currentUserId = User.Identity.GetUserId();
    ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
    Event currentEvent = db.Events.Find(model.EventID);

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

    };
    float total = 0;

    // Initialise a TicketsOrdered data model for each valid ticket
    foreach (TicketVm ticket in model.Tickets.Where(t => t.Quantity > 0))
    {
        total += (ticket.Price*ticket.Quantity);
        TicketsOrdered ticketOrder = new TicketsOrdered
        {
            OrderID = order.OrderID,
            TicketID = ticket.TicketID,
            Quantity = ticket.Quantity
        };
        db.ticketsOrdered.Add(ticketOrder);
    }

    order.OrderTotal = total;
    currentEvent.Order.Add(order);
    currentUser.Order.Add(order);


    SummaryVm summaryVm = new SummaryVm
    { 
        email = order.user.UserName, 
        orderID = order.OrderID,
        tickets = model.Tickets, 
        totalPrice = total, 
        eventID=model.EventID,
        stripePublishableKey=ConfigurationManager.AppSettings["stripePublishableKey"]
    };

    db.Orders.Add(order);
    db.SaveChanges(); 
    return RedirectToAction("OrderSummary", "Order", summaryVm);
}

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 IList<TicketVm> Tickets { get; set; }
    public Event events {get; set;}
}

public class SummaryVm
{

    public IList<TicketVm> tickets { get; set; }
    public string email { get; set; }
    public float totalPrice { get; set; }
    public int orderID { get; set; }
    public int eventID { get; set; }
    public string stripePublishableKey { get; set; }
    public string StripeToken { get; set; }
    public string StripeEmail {get;set;}

}

Upvotes: 0

Views: 7500

Answers (1)

Ion Sapoval
Ion Sapoval

Reputation: 635

Move SaveChanges before creating SummaryVm

...
db.Orders.Add(order);
db.SaveChanges(); 

SummaryVm summaryVm = new SummaryVm
{ 
    email = order.user.UserName, 
    orderID = order.OrderID,
    tickets = model.Tickets, 
    totalPrice = total, 
    eventID=model.EventID,
         stripePublishableKey=ConfigurationManager.AppSettings["stripePublishableKey"]
};

return RedirectToAction("OrderSummary", "Order", summaryVm);

and it will work.

Upvotes: 2

Related Questions