gpt4 book ai didi

javascript - 如何使用 ajax 调用更新/更改数据库值而不刷新页面

转载 作者:行者123 更新时间:2023-12-03 03:31:02 25 4
gpt4 key购买 nike

我是 mvc 的初学者,正在开发一个电子商务网站。在我的购物车列表中,我需要输入具体数量来计算总价,而无需刷新页面。我在 foreach 语句中使用了 java 脚本调用,它起作用了,就像当我输入数量并单击“TotalPrice”字段时,价格将自动计算并显示。

但是现在,当我点击“TotalPrice”字段时,我需要使用新输入的数量和总价来更新我的数据库

我正在尝试ajax,但不熟悉它

我的看法

                <table class="table">

@{
int i = 0;

foreach (var item in lscart)
{
i = i + 1;
<tr>
<td>@Html.HiddenFor(modelItem => item.CProductID, new { htmlAttributes = new { @class = "form-control", @id = "pid" + @i } })</td>
<td>
@{
var base64 = Convert.ToBase64String(item.CImage);
var imgSrc = String.Format("data:image/jpg;base64,{0}", base64);
<img src='@imgSrc' style="width:100px; height:100px;" />
}
</td>
<td>@Html.DisplayFor(modelItem => item.CProductName)</td>
<td>Rs.</td>
<td>@Html.EditorFor(modelItem => item.CPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @id = "cprice" + @i } })</td>

<td>
<div class="col-md-6">

@Html.EditorFor(modelItem => item.Cquantity, new { htmlAttributes = new { @class = "form-control", @placeholder = " Qty", @id = "qt" + @i } })


</div>

</td>


<td>
<div>
@Html.EditorFor(modelItem => item.CTotalPrice, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @placeholder = " Total Price", @id = "total" + @i, @onclick = "edValueKeyPress(" + @i + ")", @onkeyup = "edValueKeyPress(" + @i + ")" } })

</div>
</td>

<td><a href="#"><i class="fa fa-close"></i></a></td>
</tr>
}
}


</table>

我的 JavaScript 无需刷新页面即可计算总价

<script>
function edValueKeyPress(i) {


//alert(i);
var edValue = document.getElementById("qt" + i);
var edValue1 = document.getElementById("cprice" + i);
var s = edValue.value * edValue1.value;
var t = document.getElementById("total" + i);
t.value = s;

}
</script>

我的ajax代码(这还没有完成,我不知道在我看来在哪里调用这个ajax函数)

<script type="text/javascript">

$(document).ready(function () {
alert('ready');
var quantity = document.getElementById("qt" + i);
var proid = document.getElementById("pid" + i);
var price = document.getElementById("cprice" + i);
$.ajax({
url: '/Products/Cartup',
type: "GET",
data: { quantity: quantity, proid: proid, price: price },
dataType: "JSON",
success: function (rslt) {

},
error: function (rslt) {


}
});


})
</script>

我的 Controller 操作方法

 public ActionResult Cartup(int quantity, int proid,float price)
{
var Cert = (from cert in db.TBL_Cart where cert.CProductID == proid select cert).FirstOrDefault();
if (Cert != null)
{
int priceint= Convert.ToInt32(price);
Cert.Cquantity = quantity;
Cert.CTotalPrice = quantity * priceint;
db.Entry(Cert).State = EntityState.Modified;
db.SaveChanges();
}


return View();
}

我的数据库表设计

/image/mh9F7.png

我的完整源代码与数据库

https://www.dropbox.com/s/4iegq4kpsoqoue1/backup%20of%20indianhub.zip?dl=0

Controller :产品 Controller ,查看:购物车列表

最佳答案

不要添加id,而是向每个td添加一个唯一的类。例如:

@Html.EditorFor(modelItem => item.Cquantity,
new { htmlAttributes = new { @class = "quantity form-control", @placeholder = "Qty" } })


然后您可以附上change quantity 输入的处理程序。

$(document).ready(function () {
$(".quantity").change(function () {
var $tr = $(this).closest("tr"); // get the tr for that row
var productId = $tr.find('.productID').val(); // get the productID for that row
var price = $tr.find('.price').val();

$.ajax({
url: '/Products/Cartup',
type: "GET",
data: { quantity: $(this).val(), proid: productId , price: price },
dataType: "JSON",
success: function (data) { alert("success"); },
error: function (err) { }
});
});
});

需要考虑的一些事情:

  • 您的CTotalPrice只读。您不需要将任何按键事件附加到该输入。
  • 无需为 HiddenFor 元素提供 td。您可以将其移至价格td
  • db.TBL_Cart where cert.CProductID == proid:您不应该通过 CartId 获取购物车吗?我不知道您的数据库结构,但看起来您正在获得具有特定产品的第一辆购物车?不一定是您要更新的购物车。
  • cartIdproductIdquantity 发布到服务器。通过 Controller 中的 productId 获取产品的价格。使用总价更新购物车表
  • 更新表格时应使用 post 方法

(我希望您将其作为一个个人项目来学习。因为否则会存在许多设计缺陷,您的客户将免费购买所有东西并让您破产:D)

关于javascript - 如何使用 ajax 调用更新/更改数据库值而不刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com