我试图让一个人使用jQuery和AJAX从购物车中删除商品。每当我点击链接删除项目时,我都会收到500内部服务器错误。这是我的代码:
@model AccessorizeForLess.ViewModels.OrderDetailViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript">
$(function () {
// Document.ready -> link up remove event handler
$(".RemoveLink").click(function () {
// Get the id from the link
var recordToDelete = $(this).attr("data-id");
if (recordToDelete != '') {
// Perform the ajax post
$.post("/Orders/RemoveFromCart", { "id": recordToDelete },
function (data) {
// Successful requests get here
// Update the page elements
if (data.ItemCount == 0) {
$('#row-' + data.DeleteId).fadeOut('slow');
} else {
$('#item-count-' + data.DeleteId).text(data.ItemCount);
}
$('#cart-total').text(data.CartTotal);
$('#update-message').text(data.Message);
$('#cart-status').text('Cart (' + data.CartCount + ')');
});
}
});
});
</script>
<h3>
<em>Review</em> your cart:
</h3>
@*<p class="button">
@Html.ActionLink("Checkout
>>", "AddressAndPayment", "Checkout")
</p>*@
<div id="update-message">
</div>
<table>
<tr>
<th>
Product Name
</th>
<th> </th>
<th>
Price (each)
</th>
<td> </td>
<th>
Quantity
</th>
<th></th>
</tr>
@foreach (var item in Model.OrderItems)
{
<tr id="row-@item.ProductId">
<td>
@Html.ActionLink(item.Product.ProductName, "Details", "Products", new { id = item.ProductId }, null)
</td>
<td> </td>
<td>
@item.ProductPrice
</td>
<td> </td>
<td id="item-count-@item.ProductQuantity">
@item.ProductQuantity
</td>
<td> </td>
<td>
<a href="#" class="RemoveLink" data-id="@item.ProductId">Remove</a>
</td>
</tr>
}
<tr>
<td>
Total
</td>
<td></td>
<td></td>
<td id="cart-total">
@Model.OrderTotal
</td>
</tr>
</table>
修改
以下是控制器中remove方法的代码:
[HttpPost]
public ActionResult RemoveFromCart(int id)
{
// Remove the item from the cart
var cart = ShoppingCart.GetCart(this.HttpContext);
// Get the name of the product to display confirmation
string productName = db.OrderItems
.FirstOrDefault(item => item.ProductId == id).Product.ProductName;
// Remove from cart
int itemCount = cart.RemoveFromCart(id);
// Display the confirmation message
var results = new ShoppingCartRemoveViewModel
{
Message = Server.HtmlEncode(productName) +
" has been removed from your shopping cart.",
CartTotal = cart.GetTotal(),
CartCount = cart.GetCount(),
ItemCount = itemCount,
DeleteId = id
};
return Json(results);
}
以下是上一个方法中引用的 RemoveFromCart 的代码:
public int RemoveFromCart(int id)
{
// Get the cart
var cartItem = entities.Orders.FirstOrDefault(
c => c.OrderGUID == ShoppingCartId
&& c.OrderItems.Where(p => p.ProductId == id).FirstOrDefault().ProductId == id);
int itemCount = 0;
int? cartItemConut = cartItem.OrderItems.Where(p => p.ProductId == id).FirstOrDefault().ProductQuantity;
if (cartItem != null)
{
if (cartItemConut > 1)
{
cartItemConut--;
itemCount = (int)cartItemConut;
}
else
{
OrderItem oi = cartItem.OrderItems.Where(x => x.ProductId == id).FirstOrDefault();
entities.OrderItems.Remove(oi);
}
// Save changes
entities.SaveChanges();
}
return itemCount;
}
以下是 ShoppingCartRemoveViewModel
的代码using System.ComponentModel.DataAnnotations;
namespace AccessorizeForLess.ViewModels
{
public class ShoppingCartRemoveViewModel
{
public string Message { get; set; }
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal CartTotal { get; set; }
public int CartCount { get; set; }
public int ItemCount { get; set; }
public int DeleteId { get; set; }
}
}
如果您需要更多
,请告诉我答案 0 :(得分:0)
这是一个权限错误,进入并给出了控制器文件夹上的ASPNET帐户权限,错误就消失了。
答案 1 :(得分:0)
500服务器错误意味着您的代码中存在异常。我从你的代码中猜测cartitem或oi(orderitem)是null并且你试图对那些做一个LINQ查询。在尝试进行查询之前先检查它们是否为null。