如何在表单提交时将 foreach 中的 viewbag 值从视图传递到控制器

How to pass viewbag values in a foreach from view to controller on form submit

在我的 ViewCart.cshtml 页面中,显示了用户添加的产品数量并通过 foreachloop 显示.我想在表单提交传递所有这些产品ID一些表单数据(额外)。 作为新手,我不知道如何传递数据列表。

Viewcart.cshtml

@foreach(var p in ViewBag.product)
{
    <tr class="rem1">
        <td class="invert">1</td>
        <td class="invert-image">
            <a href="single_product.html">
                <img src="@Url.Content(p.img)" alt=" " height="75" class="img-responsive">
            </a>
        </td>
        <td class="invert">
            <div class="quantity">
                <div class="quantity-select">
                    <div class="entry value-minus" id="minus">&nbsp;</div>
                    <div class="entry value" id="counter">1</div>
                    <div class="entry value-plus active" id="plus" onclick="func()">&nbsp;</div>
                </div>
            </div>
        </td>
        <td class="invert" onclick="as()">@p.mname</td>

        <td class="invert" id="prize">@p.mprize</td>
        <td class="invert">
            <div class="rem">
                <div class="close1"> </div>
            </div>

        </td>
    </tr>
}

我的模态类

 public class items
    {
        public int mid { get; set; }
        public string mtype { get; set;}
        public string mname{ get; set; }
        public string mcode{ get; set; }
        public string msize{ get; set; }
        public DateTime mdate{ get; set; }
        public string mctype{ get; set; }
        public string mdprize { get; set; }
        public string mprize{ get; set;}
        public string mstock{ get; set;}
        public string img1 { get; set; }
        public string img2 { get; set; }
        public string img3 { get; set; }
    }

你的问题是一个漫长的过程这是一个视图模型的例子,你可以按照你的需要进行修改。

public class CartViewModel
{
    public List<CartItemModel> CartItemList { get; set; }

    public decimal NetTotal { get; set; }
    public CartViewModel()
    {
        CartItemList = new List<CartItemModel>();
    }
}

public class CartItemModel
{
    public long CartId { get; set; }
    public long ProductId { get; set; } 
    public string ProductName { get; set; }
    public string ProductImage { get; set; }
    public string VariantName { get; set; }
    public string VariantGroup { get; set; }
    public short Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal DiscountedPrice { get; set; }
    public decimal TotalAmount { get; set; }
    public bool IsPromotional { get; set; }
}

并在您的视图中循环遍历此视图模型