【发布时间】:2020-06-24 02:59:21
【问题描述】:
我的模型中有一些数据,模型中有一些图像。来自数据库的图像,我想在单击按钮上使用 jquery 将该图像添加到我的购物车中。我正在获取除图像之外的所有数据,这是代码。模型。图像是字符串图像列表,我只想要一个图像在我的字符串图像购物车类中。帮帮忙。
@{
@model MobileModel
}
<button type="submit" class="addcartbtn mt-5" data-pid="@Model.Id" data-pname="@Model.Name" data-pprice="@Model.Price" data-purl="@Model.Images" data-pqty="1">
<span class=" fa fa-shopping-cart"> Add To Cart</span>
</button>
Jquery 代码
$(".addcartbtn").click(function (e) {
e.preventDefault();
var obj = {
"Id": $(this).data("pid"),
"Name": $(this).data("pname"),
"Price": $(this).data("pprice"),
"Images": $(this).data("purl"),
"Quantity": $(this).data("pqty")
}
$(this).parents("#proditem").fadeOut(300);
$.ajax(
{
url: "/Cart/Add",
type: "GET",
data: obj
}
).done(function (itemscount) {
$("#cartitems").text(itemscount);
});
购物车类
public class ShoppingCartItem
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public string Images { get; set; }
public int Quantity { get; set; }
public int Amount { get {return Quantity * Price;}}
}
我要发送数据的购物车控制器
public class CartController : Controller
{
[HttpGet]
public int Add(ShoppingCartItem item)
{
ShoppingCart cart = HttpContext.Session.Get<ShoppingCart>(WebUtil.Cart);
if (cart == null) cart = new ShoppingCart();
cart.Add(item);
HttpContext.Session.Set(WebUtil.Cart, cart);
return cart.NumberOfItems;
}
数据传入的MobileModel类表单
public class MobileModel
{
public MobileModel()
{
Images = new List<string>();
}
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public List<string> Images { get; set; }
}
【问题讨论】:
-
欢迎来到stackoverflow。您面临的问题是什么?是什么阻碍了你?
-
@Model.Id、@Model.Name 等的类型是什么?可以给我们你的code behind code和更详细的view code吗?
-
@YongqingYu 立即查看..
-
@Ahmad 我无法将图像发送到控制器。
标签: c# jquery ajax asp.net-core razor-pages