无法成功从数据库中删除记录 - c#

难道我做错了什么?一切似乎都还可以,但是记录不会从数据库中删除:

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#代码有问题。为什么不从数据库中删除?

参考方案

我有一些建议。

在这些情况下,总是很可能其中一个请求项与您认为的不完全一样,因此操作或报价ID可能都包含一个惊喜。您可以使用断点检查那些对象。

下一个要检查的项目是myModel.Quote quotes包含匹配的报价对象。即使ID正确(例如,它未链接到当前用户,或者产品类型不同,等等),您可能拥有的一个过滤器仍将其排除在外。

我注意到您正在使用...

database.Quotes.DeleteObject(quotes);
database.SaveChanges();

也就是说,您在第一个而不是第二个上使用Quotes:是否应在相同的上下文中放置SaveChangesDeleteObject调用?

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

单击选项卡链接时,请专注于每个引导选项卡中的First asp:textbox - javascript

我是开发的新手,并开始开发简单的asp.net应用程序。我正在使用每个都有一些asp标签和文本框的bootstrap选项卡。单击该选项卡时,我要重点关注选项卡内容中的第一个文本框。我搜索了各种答案,但都是针对输入字段的(exp:输入type =“ text”)。找不到适用于ASP文本框的任何内容。任何帮助将不胜感激。谢谢 javascript参考方案 ASP…

Javascript-Urls的奇怪字符串比较行为 - javascript

最近,在编写我无法理解的javascript时遇到了字符串比较的问题。我从完全相同的网址创建了两个字符串,当我比较它们时返回false,但是在重新分配相同的字符串后,比较返回true。这是我的示例:var str1 = "http://google.com/"; var str2 = "http://google.com‏/&#…

ASP.NET-如何更改JSON序列化的方式? - javascript

我正在使用ASP.NET通过以下查询返回Json文件:public ActionResult getTransactionTotals(int itemID) { DBEntities db = new DBEntities(); var query = from trans in db.Transactions // Linq query removed …