I try to fill my model that is member of view model, I send data from Action to view.It's work correct. but when click on save button, get an error that
Object reference not set to an instance of an object.
The view model is
public class OrderCustomer
{
public Order Order { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
And action for post data is
[HttpPost]
public ActionResult AddressAndPayment(OrderCustomer values)
{
var orderCustomer = new OrderCustomer();
var order = orderCustomer.Order;
string uId = User.Identity.GetUserId();
order.Username = User.Identity.Name;
order.ApplicationUserId = uId;
order.OrderDate = DateTime.Now;
order.Address = values.ApplicationUser.Address;
order.CityId = (int)values.ApplicationUser.CityId;
order.CountryId = (int)values.ApplicationUser.CountryId;
order.Email = values.ApplicationUser.Email;
order.FirstName = values.ApplicationUser.FName;
order.LastName = values.ApplicationUser.LName;
order.Phone = values.ApplicationUser.Phone;
order.PostalCode = values.ApplicationUser.PostalCode;
order.ProvinceId = (int)values.ApplicationUser.ProvinceId;
if (ModelState.IsValid)
{
TryUpdateModel(order);
try
{
_db.Orders.Add(order);
_db.SaveChanges();
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.CreateOrder(order);
return RedirectToAction("Index", "Payment", new { id = order.Id });
}
catch
{
return View(order);
}
}
return View(order);
}
when debugger arrive on this code show the error line 5 in above code that show in below, how can i fix it?
order.Username = User.Identity.Name;