我有一个控制器和视图,允许用户“结帐”购物车的内容。
当我的 CheckoutController 将 Order 模型返回到 Checkout 视图时,一切正常。但是,我现在还想在 Checkout 视图上显示购物车的内容,因此修改了 Controller 和 View 以使用包含 Order 和 CartItems 的视图模型。
我创建了一个 CheckoutViewModel 并修改了 GET: 和 POST: ActionResults 以使用这个 viewmodel。
问题是在 POST 期间未正确填充 CheckOutViewModel:
我在“checkoutViewModel.Order.Username = User.Identity.Name;”这一行收到“对象引用未设置为对象实例”错误 在 POST: ActionResult 但我不确定到底是什么没有被实例化。
如果需要,我可以发布使用 Oreder 模型而不是 CheckoutViewModel 的原始工作代码。
我确定我有一些简单的结构或语法问题,我只是不知道它是什么,因为我仍在努力了解在 MVC 中做事的最佳方式。
获取:操作结果
//GET: /Checkout/AddressAndPayment
public ActionResult AddressAndPayment()
{
//To pre-populate the form, create a new Order object and get the ShoppingCart, populate the ViewModel and pass it to the view
var order = new Order();
order.Username = User.Identity.Name;
MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
storeDB.SaveChanges();
var cart = ShoppingCart.GetCart(this.HttpContext);
// Set up our ViewModel
var viewModel = new CheckoutViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal(),
Order = order
};
// Return the view
return View(viewModel);
}
POST:动作结果
//POST: /Checkout/AddressAndPayment
[HttpPost]
public ActionResult AddressAndPayment(FormCollection values)
{
var checkoutViewModel = new CheckoutViewModel();
TryValidateModel(checkoutViewModel);
try
{
checkoutViewModel.Order.Username = User.Identity.Name;
checkoutViewModel.Order.OrderDate = DateTime.Now;
//Save Order
storeDB.Orders.Add(checkoutViewModel.Order);
storeDB.SaveChanges();
//Process the order
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.CreateOrder(checkoutViewModel.Order);
storeDB.SaveChanges();
//Send 'Order Confirmation' email
ViewData["order"] = checkoutViewModel.Order;
UserMailer.OrderConfirmation(checkoutViewModel.Order).SendAsync();
return RedirectToAction("Complete", new { id = checkoutViewModel.Order.OrderID });
}
catch
{
//Invalid - redisplay with errors
return View(checkoutViewModel);
}
}
看法
@model OrderUp.ViewModels.CheckoutViewModel
@{
ViewBag.Title = "AddressAndPayment";
}
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<h2>Pick Up Details</h2>
<fieldset>
<legend>When are you gonna be hungry?</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Order.Phone)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Order.Phone)
@Html.ValidationMessageFor(model => model.Order.Phone)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Order.PickUpDateTime)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Order.PickUpDateTime)
@Html.ValidationMessageFor(model => model.Order.PickUpDateTime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Order.Notes)
</div>
<div class="editor-multiline-field">
@Html.EditorFor(model => model.Order.Notes)
@Html.ValidationMessageFor(model => model.Order.Notes)
</div>
</fieldset>
<input type="submit" value="Submit Order" />
}
<div style="height:30px;"></div>
<h3>
<em>Review</em> your cart:
</h3>
<table>
<tr>
<th>
Menu Item
</th>
<th>
Price (each)
</th>
<th>
Notes
</th>
<th>
Quantity
</th>
<th></th>
</tr>
@foreach (var item in Model.CartItems)
{
<tr id="row-@item.RecordID">
<td>
@Html.ActionLink(item.MenuItem.Name, "Details", "Store", new { id = item.MenuItemID }, null)
</td>
<td>
@Html.DisplayFor(modelItem => item.MenuItem.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Notes )
</td>
<td id="item-count-@item.RecordID">
@item.Count
</td>
<td>
</td>
</tr>
}
<tr>
<td >
Total
</td>
<td id="cart-total">
@String.Format("${0:F2}", Model.CartTotal)
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>