难道我做错了什么?一切似乎都很好,但记录并没有从数据库中删除:
C#
@{
WebSecurity.RequireAuthenticatedUser();
myModel.OSFEntities database = new myModel.OSFEntities();
var productInformation = Request["Pi"];
int productId = Convert.ToInt32(productInformation.Substring(0, productInformation.LastIndexOf("-")));
string productType = productInformation.Substring(productInformation.LastIndexOf("-", productInformation.Length)).Replace("-", String.Empty);
var userId = WebSecurity.CurrentUserId;
DateTime date = DateTime.Now;
int quantity = 1;
string quoteId = "";
if (Request["Action"] == "Remove")
{
if (Session["QuoteId"] != null)
{
quoteId = (String)Session["QuoteId"];
myModel.Quote quotes = database.Quotes.FirstOrDefault(
q => q.UserId == userId && q.QuoteId == quoteId && q.ProductId == productId && q.ProductType == productType);
database.Quotes.DeleteObject(quotes);
database.SaveChanges();
}
}
}
打字稿:
function RemoveProductFromCart(e) {
// Remove from their Cart.
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// The data is now stored in the responseText.
// Change value in textbox to 0 so user knows it's been
// removed from their cart.
var el = <HTMLInputElement>document.getElementById(e.id + "-TB");
if (el != null) {
el.value = "0";
}
}
else {
// Server responded with a different response code.
alert(xhr.statusText);
}
}
}
var parameters = "Pi=" + encodeURIComponent(e.id) + "&" + "Action=Remove";
xhr.open("POST", "../Cart.cshtml");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", parameters.length.toString());
xhr.setRequestHeader("Connection", "close");
xhr.send(parameters);
return e.id;
}
在使用开发工具进行调试时F12,我看到:
...这让我相信我的 C# 代码有问题。为什么它不会从数据库中删除?