我做錯了什麼?一切似乎是美好的,但記錄中沒有得到從數據庫中刪除:無法從數據庫中成功刪除記錄
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#代碼錯了。爲什麼它不會從數據庫中刪除?
我會** **強烈建議您將所有業務邏輯從您的視圖中取出並存入*至少*控制器中...... – James
這是WebPages **而非MVC **。是的,我儘可能地分開了所有的東西。 – Arrow
對不起,上面的代碼看起來像MVC語法可疑。你有沒有證實你的報價查詢實際上是返回的東西&不爲空? – James