Razor View Row ID, всегда пропускающий ID только для первой строки - PullRequest
0 голосов
/ 02 марта 2020

У меня есть ASP. NET Core MVC Razor view, в котором я пытаюсь передать идентификатор строки моему контроллеру. Однако я могу передать только идентификатор строки для первой строки, независимо от того, какую строку я выбрал. Я пытаюсь обновить инвентарь для примера приложения, над которым я работаю. Мне нужно передать в строке номер идентификатора 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");
    }

1 Ответ

1 голос
/ 04 марта 2020

Вам нужно изменить местоположение формы, вот рабочая демонстрация, как показано ниже:

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");
}

Результат: enter image description here

...