c# - 如何在模型到模型中使用 jquery 发送数据和图像。?
问题描述
我想将 Model.Images 中的价格、名称 ID 和第一张图片发送到我的购物车控制器,但它不起作用我的 Model.Images 中有 3 张图片告诉我如何解决这个问题。
总结控制器
public IActionResult Summery(int id)
{
MobileModel m = new MobileHandler().GetMobile(id).ToModel();
return View(m);
}
数据传入的 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; }
}
总结视图
@{
@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[0]" 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);
});
CartModel 类
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;
}
解决方案
推荐阅读
- java - 来自 application.yaml 的休眠参数注释值
- linux - 为什么两个独立的进程可以 bind() 到同一个 TCP 端口,但不能同时监听?
- mongodb - 为什么从集合中删除许多记录后索引大小会增加?
- javascript - 如何在该类的实例的属性上调用类方法?
- mysql - SQL - 试图判断查询的哪一部分与“或”语句匹配
- java - 从测试中获取 JUNIT 结果以将其结果发布到 testrail
- angular - 无法在另一个组件中获取和显示数据
- sql - 从用户定义的表类型中读取
- c# - AddRange 直接来自 `await`
- docker - Dockerfile:FastCGI Throws Primary script unknown”,同时从上游错误读取响应标头