【发布时间】:2012-10-19 20:23:39
【问题描述】:
我做错了吗?一切似乎都正常,但记录并没有从数据库中删除:
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# 代码有问题。为什么它不会从数据库中删除?
【问题讨论】:
-
我会强烈建议您将所有业务逻辑从您的视图中移出并至少控制器...
-
抱歉,上面的代码看起来很像 MVC 语法。您是否验证过您对报价的查询实际上返回了一些东西而不是空值?
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
我在这里已经 3 年多了,还没有看到人们在使用标签时遇到问题。如果他们不阅读您的问题,那么他们将不会有太大帮助。
-
这句话有子关系吗?或引用此报价的任何实体?如果有,需要先删除
标签: c# javascript asp.net entity-framework typescript