【问题标题】:Submit the value of each item of list to controller (MVC)将列表中每一项的值提交给控制器(MVC)
【发布时间】:2020-09-11 12:21:33
【问题描述】:

我正在尝试制作某种购物车,因此我在视图中显示了一个商品列表。

我添加了一个带有“+ Add”值的提交按钮和一个带有显示的每个项目的值金额的数字输入。

这是我的代码:

查看:

@model MyProject.ViewModel.AddProductsViewModel

@{
    Layout = null;
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<table class="table" align="left" style="padding-left:15px">
    <tr>
        <th>
            Product
        </th>
    </tr>

    @foreach (var item in Model.ProductsList)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Descripcion)
            </td>
            <td>
                <input type="hidden" name="ProductId" value="@item.ProductId" />                                                                       
                <input type="number" id="Amount" name="Amount"
                       min="1" max="30" value="1">
                <input type="submit" value="+ Add" class="btn btn-primary" />
            </td>
        </tr>
    }
</table>
}

模型类:

public class AddProductsViewModel
{
    ...stuff here...
          
    public List<Products> ProductsList { get; set; }
}

我需要我的控制器从我单击提交到的项目中获取productId,但这样我只能获取列表中第一项的 ID。我怎样才能做到这一点?

【问题讨论】:

  • 您需要为项目名称使用索引(将您的foreach 切换为for 循环并在表单字段的name 属性中使用索引器,以便名称属性为每个输入都是唯一的)
  • 这能回答你的问题吗? ASP.Net MVC - model with collection, Submit
  • 我在上一篇关于这个问题的帖子中得到了相同的答案,但在那个解决方案中,我不明白为什么他们将这个值赋予隐藏的输入 @devlincarnate

标签: c# .net asp.net-mvc


【解决方案1】:
@{int i = 0;}
@foreach (var item in Model.ProductsList)
{

    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Descripcion)
        </td>
        <td>
            <input type="hidden" name="ProductsList[@i].ProductId" value="@item.ProductId" />                                                                       
            <input type="number" id="Amount" name="ProductsList[@i].Amount"
                   min="1" max="30" value="1">
            <input type="submit" value="+ Add" class="btn btn-primary" />
        </td>
    </tr>
     @(i++)
}

【讨论】:

    【解决方案2】:

    刚刚通过在循环内开始表单解决了这个问题,如下所示:

    @model MyProject.ViewModel.AddProductsViewModel
    
    @{
        Layout = null;
    }
    
    
        @Html.AntiForgeryToken()
    
    <table class="table" align="left" style="padding-left:15px">
        <tr>
            <th>
                Product
            </th>
        </tr>
    
        @foreach (var item in Model.ProductsList)
        {
    using (Html.BeginForm())
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Descripcion)
                </td>
                <td>
                    <input type="hidden" name="ProductId" value="@item.ProductId" />                                                                       
                    <input type="number" id="Amount" name="Amount"
                           min="1" max="30" value="1">
                    <input type="submit" value="+ Add" class="btn btn-primary" />
                </td>
            </tr>
        }
    }
    </table>
    

    【讨论】:

    • 这是一个 hack,并不能解决您的 HTML 无效(具有相同 id 的多个字段)的问题,而且通常不是发布购物车的方式。 .逐项???
    • 同意,您可能希望使用 javascript/AJAX 添加到卡/更新数量而无需完整回发
    • 任何 ajax 或 js 指南来解决这个问题? @GlynnHurrell
    • 首先在您选择的搜索引擎中搜索“asp net mvc ajax post tutorial”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多