简体   繁体   English

如何从 http 帖子中将列表 model 传递到 controller

[英]How to pass List model to controller from view http post

I have a CategoryModel that contains a list item for the ItemModel as well as some other strings etc.我有一个CategoryModel ,其中包含ItemModel的列表项以及其他一些字符串等。

I'm trying to pass the list from the view to the controller.我正在尝试将视图中的列表传递给 controller。 The model on the view is the ABCModel which contains a list of 'Categories' of type CategoryModel .视图上的 model 是ABCModel ,其中包含CategoryModel类型的“类别”列表。 On the view I'm only looking to post the 'active' Category back to be saved.在视图中,我只想将“活动”类别发布回去以进行保存。

When I do this the active category is passed with details added from the form but the list of items is null.当我这样做时,将传递活动类别并从表单中添加详细信息,但项目列表是 null。

.cs Models .cs 模型

    namespace Company.Models
    {
        public class ABCModel
        {
            public IList<CategoryModel> Categories;
            public SummaryModel Summary;
        }
    }

    namespace Company.Models
    {
    public class CategoryModel
    {
        public string Label { get; set; }
        public bool Active { get; set; }
        public decimal Amount { get; set; }
        public string Frequency { get; set; }
        public string Type { get; set; }
        public List<ItemModel> Items;
    }
    )

    namespace Company.Models
    {
    public class ItemModel
    {
        public string Label { get; set; }
        public string Explanation { get; set; }
        public string Amount { get; set; }
        public string Frequency { get; set; }
        public bool Flag { get; set; }
        public string Note { get; set; }
    }
    }

Controller Controller

    [HttpPost]
        public ActionResult SaveItems([FromForm] CategoryModel activeCategoryModel, [FromForm] List<ItemModel> items, [FromQuery] string activecategory, [FromQuery] string nextcategory)
        {
        ...
        }

View看法

    @model ABCModel

    @{ CategoryModel nextCategory = null; }
    @{ CategoryModel activeCategoryModel = null; }

    @if (Model.Categories.Any())
    {
        @for (var i = 0; i < Model.Categories.Count; i++)
        {
            Company.Models.CategoryModel category = Model.Categories[i];

            @if (category.Active)
            {
                activeCategoryModel = category;
                if ((i + 1 < Model.Categories.Count) && (Model.Categories[i + 1] != null))
                {
                    nextCategory = Model.Categories[i + 1];
                }
            }
        }
    }

    <form id="ABC-form" class="needs-validation" novalidate="" asp-action="SaveItems" asp-controller="Home" method="post">

            @if (activeCategoryModel != null)
            {
                <input asp-for="@activeCategoryModel.Label" type="hidden" />
                <input asp-for="@activeCategoryModel.Active" type="hidden" />
                <input asp-for="@activeCategoryModel.Amount" type="hidden" />
                <input asp-for="@activeCategoryModel.Frequency" type="hidden" />
                <input asp-for="@activeCategoryModel.Type" type="hidden" />

                @if (activeCategoryModel.Items.Any())
                {    
                    @for (var i = 0; i < activeCategoryModel.Items.Count; i++)
                    {
                        @Html.HiddenFor(x => activeCategoryModel.Items[i].Label)
                        @Html.HiddenFor(x => activeCategoryModel.Items[i].Explanation)
                        @Html.TextBoxFor(x => items[i].Amount, new { @class = "form-control" })
                    }
                }
            }
        <button id="continue" type="submit" asp-action="SaveItems" asp-route-activecategory="@activeCategoryModel.Label" asp-route-nextcategory="@(nextCategory != null ? nextCategory?.Label : null)">Continue</button>
    </form>

I had been including IFormCollection as a parameter within the SaveItems on the controller and this showed that the items are being passed in the format I was thinking would work but the model only shows the values entered and the list of items is null.我一直将IFormCollection作为参数包含在 controller 上的 SaveItems 中,这表明项目以我认为可行的格式传递,但 model 仅显示输入的值,项目列表为 Z37A6259CC0489DAE200Z37B9Z7866

IFormCollection 在控制器上的调试中悬停

activeCategoryModel 在控制器上的调试中悬停 - items = null

How can I populate the list of items on the active category model from the view?如何从视图中填充活动类别 model 上的项目列表?

Items in CategoryModel should be a property not a filed,so your CategoryModel should be like below: CategoryModel中的Items应该是属性而不是归档,因此您的CategoryModel应该如下所示:

public class CategoryModel
{
    public string Label { get; set; }
    public bool Active { get; set; }
    public decimal Amount { get; set; }
    public string Frequency { get; set; }
    public string Type { get; set; }
    public List<ItemModel> Items { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM