1
我嘗試填充作爲視圖模型成員的模型, 我從Action發送數據進行查看。它的工作正確。但是當點擊保存按鈕時,出現如下錯誤:在mvc中填充我的m viewModel的模型
未將對象引用設置爲對象的實例。
視圖模型是
public class OrderCustomer
{
public Order Order { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
併爲後期的數據動作
[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);
}
當調試器在這個代碼來顯示在上面的代碼中的錯誤行5,在下面展示,如何我可以修復它嗎?
order.Username = User.Identity.Name;
也許請求不被認證,所以沒有屬性集合 - 看看你的授權碼或設置一個匿名委託人這樣https://stackoverflow.com/ question/2893334/invalidating-asp-net-formsauthentication-server-side – mchan
'order'爲'null'。您不會將實例分配給該屬性 – Nkosi