简体   繁体   English

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

[英]Cannot successfully delete record from the database

Am I doing something wrong? 难道我做错了什么? Everything seems to be OK, but the record doesn't get deleted from the database: 一切似乎都还可以,但是记录不会从数据库中删除:

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();
        }
    }
}

TypeScript: 打字稿:

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;
}

While debugging with F12 Dev Tools, I see: 使用F12开发工具进行调试时,我看到:

在此处输入图片说明

... which leads me to believe that it's something wrong with my C# code. ...这使我相信我的C#代码有问题。 Why won't it delete from the database? 为什么不从数据库中删除?

I have a few suggestions. 我有一些建议。

In these cases it is always fairly likely that one of the request items isn't exactly what you thought it was, so either the action or the quote id could contain a surprise. 在这些情况下,总是很可能其中一个请求项与您认为的不完全一样,因此操作或报价ID可能都包含一个惊喜。 You could check those using a breakpoint. 您可以使用断点检查那些对象。

The next item to check is that myModel.Quote quotes contains the matched quote object. 下一个要检查的项目是myModel.Quote quotes包含匹配的引号对象。 Perhaps one of the filters you have there is ruling it out even though the id is correct (for example it isn't linked to the current user, or the product type is different etc). 即使ID正确(例如,它未链接到当前用户,或者产品类型不同,等等),您可能拥有的一个过滤器仍将其排除在外。

I notice you are using... 我注意到您正在使用...

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

ie you use Quotes on the first, not on the second: Should the SaveChanges and DeleteObject calls not be placed against the same context? 也就是说,您在第一个而不是第二个上使用Quotes :是否不应将SaveChangesDeleteObject调用放在同一上下文中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM