主细节中的 ArgumentOutOfRangeException - ASP.NET Core MVC

问题描述 投票:0回答:2

我有 Master Detail 模型 Stock 和 Article,其中 StockId 作为 Article 中的外键。我在表格中显示 Stock,我想创建一个局部视图以将 Article 添加到表格的每一行。这是我试过的:

股票管理员:

public IActionResult AddArticle(int id)
    {
        Stock stock = _dbcontext.Stock.Where(e => e.StockId == id).FirstOrDefault();
        stock.Articles.Add(new Article() { ArticleId = 1 });
        return PartialView("_AddArticlePartialView", stock);
    }

    public IActionResult AddArticle(Article article)
    {
        if (article != null)
        {
            _dbcontext.Article.Add(article);
            _dbcontext.SaveChanges();
            return RedirectToAction("Index");
        }
        return View();           
    }

Index.cshtml:

@model IEnumerable<Stock>

@{
    ViewData["Title"] = "Stock";
    Layout = "~/Views/Shared/_Theme.cshtml";
 }
<table class="table table-bordered table-hover" id="display">
                <thead>
                    <tr>
                        <th>
                            @Html.DisplayNameFor(model => model.CategoryId)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Designation)
                        </th>
                        <th>
                            @Html.DisplayNameFor(model => model.Quantite)
                        </th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => item.Category.CategoryName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Designation)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Quantite)
                            </td>

                            <td>
                                <div class="btn-group-sm">
                                    <button class="btn btn-primary" data-toggle="modal" data-target="@("#AddArticle-"+item.StockId)" data-url="@Url.Action($"AddArticle/{item.StockId}")"><i class="fa fa-plus"></i>Ajouter Articles</button>
                                    @await Html.PartialAsync("_AddArticlePartialView", item)
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

_AddArticlePartialView.cshtml:

    @model StockProject.Models.Stock

@{
    ViewData["Title"] = "_AddArticlePartialView";

}

<div class="modal fade" role="dialog" tabindex="-1" id="@("AddArticle-"+Model.StockId)" aria-labelledby="addArticleLabel" aria-hidden="true" >
    <div class="modal-dialog modal-xl modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">Articles</h3>
            </div>
            <div class="modal-body">
                <form asp-action="AddArticle" method="post">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Articles</h3>

                        </div>
                        
                        <div class="card-body">
                            <table class="table table-bordered" id="articleTable">
                                <thead>
                                    <tr>
                                        <th>Numero de Serie</th>
                                        <th>Marque</th>
                                        <th>Etat</th>                                        
                                    </tr>
                                </thead>
                                <tbody>
                                    @for (int i = 0; i < Model.Quantite; i++)
                                    {
                                        <tr>
                                            <td>
                                                @Html.EditorFor(x => x.Articles[i].NumeroSerie, new { htmlAttributes = new { @class = "form-control" } })
                                            </td>

                                            <td>
                                                @Html.EditorFor(x => x.Articles[i].Marque, new { htmlAttributes = new { @class = "form-control" } })
                                            </td>
                                            <td>
                                                @Html.DropDownListFor(x => x.Articles[i].Etat, new List<SelectListItem> {
                                                       new SelectListItem { Value = "En Marche" , Text = "En Marche" },
                                                       new SelectListItem { Value = "En Panne" , Text = "En Panne" },

                                                    },
                                                  new { @class = "form-control" })
                                            </td>
                                            
                                        </tr>
                                    }

                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
                        <button type="submit" class="btn btn-primary">Sauvegarder</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

当我运行此代码时,我收到此错误消息:

指数超出范围。必须为非负数且小于集合的大小。 (参数“索引”)

错误在这一行

@Html.EditorFor(x => x.Articles[i].NumeroSerie, new { htmlAttributes = new { @class = "form-control" } })

那么我的代码有什么问题?

asp.net-mvc asp.net-core master-detail outofrangeexception
2个回答
0
投票
@Html.EditorFor(x => x.Articles[i].NumeroSerie, new {@class = "form-control"})

0
投票

在您的局部视图中,您使用

@for (int i = 0; i < Model.Quantite; i++)
遍历
Quantite
属性并获取索引,然后使用该索引从
Articles
属性中获取值。但是如果
Quantite
的计数大于
Articles
的计数,
i
就会大于
Articles
中的索引,
Articles[i]
就会报这个错误。

所以这里可以尝试使用

@for (int i = 0; i < Model.Articles; i++)
获取索引

© www.soinside.com 2019 - 2024. All rights reserved.