微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用Asp.net Core 2.2中的标签助手将视图中的对象列表发送到控制器

如何解决无法使用Asp.net Core 2.2中的标签助手将视图中的对象列表发送到控制器

我有以下视图,我想将视图中的对象列表发送到控制器,并且我使用了asp-for标记助手来进行数据绑定,但是控制器中的操作接收到空值

                                 @model IEnumerable<GoodsList>
                                <form method="post" asp-action="SubmitList" asp-controller="Submit">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th width="2%">number</th>
                                            <th width="20%">Name</th>
                                            <th width="20%">Brand</th>
                                            <th width="20%">Quantity</th>
                                            <th width="20%">Scale</th>
                                            <th width="8%">operation</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var item in Model)
                                        {
                                            <tr>
                                                <td>@item.Number</td>
                                                <td><input type="text" readonly="readonly" asp-for="@item.GoodsName" class="form-control" /></td>
                                                <td><input type="text" readonly="readonly" asp-for="@item.BrandName" class="form-control" /></td>
                                                <td><input type="text" readonly="readonly" asp-for="@item.Quantity" class="form-control" /></td>
                                                <td><input type="text" readonly="readonly" asp-for="@item.ScaleName" class="form-control" /></td>
                                                <td>
                                                    <select class="form-control" asp-items="@(new SelectList(item.Status,"Id","Name"))">
                                                    </select>
                                                </td>
                                            </tr>
                                        }
                                        <tr>
                                            <td colspan="6">
                                                <textarea class="form-control" rows="3" readonly="readonly" cols="5">@Model.Select(s => s.Description).First()</textarea>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <a class="btn btn-primary">Back</a>
                                <input type="submit" value="Submit" class="btn btn-success" style="width:auto">
                            </form>

这是我的接收空值的控制器

  [HttpPost]
    //It receives null
    public IActionResult SubmitList(IEnumerable<GoodsList> model)
     {
        return View();
    }

和模型

public class GoodsList
{
    public GoodsList()
    {
        Status = new List<ApprovalStatus>();
    }
    public int Number { get; set; }
    public string GoodsName { get; set; }
    public string BrandName { get; set; }
    public int? Quantity { get; set; }
    public string UserName { get; set; }
    public string RankName { get; set; }
    public int? RequestId { get; set; }
    public string ScaleName { get; set; }
    public IList<ApprovalStatus> Status { get; set; }
}

解决方案吗?

预先感谢

解决方法

按如下所示更改剃刀视图:

@model IEnumerable<GoodsList>
<form method="post" asp-action="SubmitList" asp-controller="Submit">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th width="2%">number</th>
                <th width="20%">Name</th>
                <th width="20%">Brand</th>
                <th width="20%">Quantity</th>
                <th width="20%">Scale</th>
                <th width="8%">operation</th>
            </tr>
        </thead>
        <tbody>
            @{ var i = 0;}
            @foreach (var item in Model)
            {
            
                <tr>
                    <td>@item.Number</td>
                    <td><input type="text" readonly="readonly" name="[@i].GoodsName" asp-for="@item.GoodsName" class="form-control" /></td>
                    <td><input type="text" readonly="readonly" name="[@i].BrandName" asp-for="@item.BrandName" class="form-control" /></td>
                    <td><input type="text" readonly="readonly" name="[@i].Quantity" asp-for="@item.Quantity" class="form-control" /></td>
                    <td><input type="text" readonly="readonly" name="[@i].ScaleName" asp-for="@item.ScaleName" class="form-control" /></td>
                    <td>
                        <select class="form-control" name="[@i].Status[0].Id" asp-items="@(new SelectList(item.Status,"Id","Name"))">
                        </select>
                    </td>
                </tr>
                i++;
            }
            <tr>
                <td colspan="6">
                    <textarea class="form-control" rows="3" readonly="readonly" cols="5">@Model.Select(s => s.Description).First()</textarea>
                </td>
            </tr>
        </tbody>
    </table>
    <a class="btn btn-primary">Back</a>
    <input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>

结果: enter image description here

另一种方式:

@model IList<GoodsList>   //change this
//..
        <tbody>
            @for(var i = 0;i<Model.Count();i++)
            {
            
                <tr>
                    <td>@Model[i].Number</td>
                    <td><input type="text" readonly="readonly"  asp-for="@Model[i].GoodsName" class="form-control" /></td>
                    <td><input type="text" readonly="readonly"  asp-for="@Model[i].BrandName" class="form-control" /></td>
                    <td><input type="text" readonly="readonly"  asp-for="@Model[i].Quantity" class="form-control" /></td>
                    <td><input type="text" readonly="readonly"  asp-for="@Model[i].ScaleName" class="form-control" /></td>
                    <td>
                        <select class="form-control" asp-for="@Model[i].Status[0].Id"  asp-items="@(new SelectList(Model[i].Status,"Name"))">
                        </select>
                    </td>
                </tr>
 
            }
            <tr>
                <td colspan="6">
                    <textarea class="form-control" rows="3" readonly="readonly" cols="5">@Model.Select(s => s.Description).First()</textarea>
                </td>
            </tr>
        </tbody>
    </table>
    <a class="btn btn-primary">Back</a>
    <input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。