我有 jquery ajax 函数来获取值作为我的控制器的参数并将 HTML 发送回可能的购物车表。
$(document).ready(function () {
$(".Quantity").change(function () {
var ProID = $(this).attr("data");
var Quatity = $(".Quantity").val();
$.ajax({
type: "GET", url: "/Cart/UpdateValue", data: { ProID: ProID, quantity: Quatity },
success: function (data) {
$(".cart_box").html(data);
}
}
);
});
});
所以在我看来
<tbody>
@foreach (var line in Model.Cart.Lines)
{
<tr>
<td>@line.Product.Name
</td>
<td style="text-align: center; padding: 0">@Html.DropDownList("Quantity", new SelectList(ViewBag.Items as System.Collections.IList, "Value", "Text", line.Quantity.ToString()), new { data=line.Product.ProductID ,@class="Quantity"})</td>
<td>@string.Format("{0:00,0 VNĐ}", line.Product.Price)</td>
<td>@string.Format("{0:00,0 VNĐ}", (line.Quantity * line.Product.Price))</td>
<td>Button</td>
</tr>
}
</tbody>`
如果我更改下拉列表中的值,它只会发生一次并且仅在第一行中发生。有人能告诉我我的错误是什么吗?编辑:这是我的购物车索引视图控制器:
public ViewResult Index(string returnUrl)
{
List<SelectListItem> items = new List<SelectListItem>();
for (int i = 1; i <= 10; i++)
{
items.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
ViewBag.Items = items;
return View(new CartIndexViewModel { Cart = GetCart(), ReturnUrl = returnUrl });
}
这是ajax请求成功时的控制器:
public PartialViewResult UpdateValue(int ProID, int quantity)
{
List<SelectListItem> items = new List<SelectListItem>();
for (int i = 1; i <= 10; i++)
{
items.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
}
ViewBag.Items = items;
Product product = repository.Products.FirstOrDefault(p => p.ProductID == ProID);
if (product != null)
{
GetCart().UpdateItem(product, quantity);
}
CartIndexViewModel ptview = new CartIndexViewModel {Cart=GetCart(),ReturnUrl="/"};
return PartialView(ptview);
}