【问题标题】:Razor View Row ID Always Passing ID for First Row OnlyRazor View Row ID 始终只为第一行传递 ID
【发布时间】:2020-03-02 19:18:36
【问题描述】:

我有一个 ASP.NET Core MVC Razor 视图,我正在尝试将行 ID 传递给我的控制器。但是,无论我选择哪一行,我都只能传递第一行的行 ID。我正在尝试更新我正在处理的示例应用程序的库存。我需要传入行 ID 号 autoId 和要删除的数量。以下是详细信息。

查看

<div id="InventoryGrid" class="container hiddenGrid">
    <div class="row">
        <form asp-action="UpdateInventory" method="post" class="container">
            <table id="autosTable" class="table table-striped text-center">
                <thead class="table-dark">
                    <tr>
                        <th>Type</th>
                        <th>Make</th>
                        <th>Model</th>
                        <th>Year</th>
                        <th>Features</th>
                        <th>Qty</th>
                        <th>Sale Price</th>
                        @*<th>Qty * Price</th>*@
                        <th>With Markup</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            
                            <td>@Html.DisplayFor(modelItem => item.AutoType)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoMake)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoModel)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoYear)</td>
                            <td>
                                @if (item.AutoFeature.Count > 0)
                                {
                                    @for (int i = 0; i <= item.AutoFeature.Count - 1; i++)
                                    {
                                        <p style="float:left;text-align:center;">
                                            @Html.DisplayFor(modelItem => item.AutoFeature[i].FeatureDescription), &nbsp;
                                        </p>
                                    }
                                }
                            </td>
                            <td>@item.StockLevel</td>
                            <td class="retailPrice">@item.RetailPrice.ToString("C", culture)</td>
                            @*<td>@item.EstTotalProfit.ToString("C", culture)</td>*@
                            <td class="calculatedMarkup"></td>
                            <td width="200">
                                <input name="id" type="text" value="@item.AutoId" class="w-25" />
                                <input name="qty" type="number" min="1" max="@(Html.DisplayFor(modelItem=> item.StockLevel))" class="fomr-control w-25"
                                       />|
                                <input type = "submit"
                                       class="btn btn-sm btn-danger"
                                       value="Remove"
                                       />
            
                            </td>
            
                        </tr>
                    }
                </tbody>
                <tfoot class="table-dark">
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td>Total: @Model.Sum(i => i.RetailPrice).ToString("C", culture)</td>
                        @*<td>Total: @Model.Sum(i => i.EstTotalProfit).ToString("C", culture)</td>*@
                        <td class="sumOfProfits"></td>
                        <td></td>
                    </tr>


                </tfoot>
            </table>
        </form>
    </div>
</div>

控制器

[HttpPost]
    public IActionResult UpdateInventory(int id, int qty)
    {

        AutosService autosService = new AutosService(_context);
        autosService.UpdateInventory(id, qty);
        return RedirectToAction("Index");
    }

【问题讨论】:

标签: asp.net-mvc asp.net-core razor asp.net-core-mvc


【解决方案1】:

您需要更改表单的位置,这是一个如下所示的工作演示:

1.型号:

public class Auto
{
    public int AutoId { get; set; }
    public string AutoType { get; set; }
    public string AutoMake { get; set; }
    public string AutoModel { get; set; }
    public string AutoYear { get; set; }
    public int StockLevel { get; set; }
    public IList<AutoFeature> AutoFeature { get; set; }
}
public class AutoFeature
{
    public string FeatureDescription { get; set; }
}

2.查看:

@model IEnumerable<Auto>
<div id="InventoryGrid" class="container hiddenGrid">
    <div class="row">
            <table id="autosTable" class="table table-striped text-center">
                <thead class="table-dark">
                    <tr>
                        //...
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>@Html.DisplayFor(modelItem => item.AutoType)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoMake)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoModel)</td>
                            <td>@Html.DisplayFor(modelItem => item.AutoYear)</td>
                            <td>
                                //...
                            </td>
                            <td>@item.StockLevel</td>
                            <td class="calculatedMarkup"></td>
                            <td width="300">
                                <form asp-action="UpdateInventory" method="post" class="container">
                                    <input name="id" type="text" value="@item.AutoId" class="w-25" />
                                    <input name="qty" type="number" min="1" max="@(Html.DisplayFor(modelItem=> item.StockLevel))" class="fomr-control w-25" />|
                                    <input type="submit"
                                           class="btn btn-sm btn-danger"
                                           value="Remove" />
                                </form>
                            </td>
                        </tr>
                    }
                </tbody>             
            </table>      
    </div>
</div>

3.控制器:

public IActionResult Index()
{
    var model = new List<Auto>()
    {
        new Auto()
        { 
            AutoId=1,AutoType="a1",AutoMake="b1",AutoModel="c1",StockLevel=3, 
            AutoFeature=new List<AutoFeature>(){ new AutoFeature() { FeatureDescription = "af1" } }
        },
        new Auto()
        {
            AutoId=2,AutoType="a2",AutoMake="b2",AutoModel="c2",StockLevel=5,
            AutoFeature=new List<AutoFeature>(){ new AutoFeature() { FeatureDescription = "af2" } }
        },
        new Auto()
        {
            AutoId=3,AutoType="a3",AutoMake="b3",AutoModel="c3",StockLevel=7,
            AutoFeature=new List<AutoFeature>(){ new AutoFeature() { FeatureDescription = "af3" } }
        }
    };
    return View(model);
}
[HttpPost]
public IActionResult UpdateInventory(int id, int qty)
{
    //do your stuff..
    return RedirectToAction("Index");
}

结果:

【讨论】:

  • 我很抱歉延迟回复,这真的很疯狂。是的,您的回答对我帮助很大,非常感谢。我确实接受了您的回答并投了赞成票,因为回复不仅有帮助而且条理清晰。
  • 漂亮的答案
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多