如何从视图 http post 将列表模型传递给控制器
How to pass List model to controller from view http post
我有一个 CategoryModel
,其中包含 ItemModel
的列表项以及一些其他字符串等
我正在尝试将列表从视图传递到控制器。视图上的模型是 ABCModel
,它包含类型 CategoryModel
的 'Categories' 列表。在视图中,我只希望 post 'active' 类别返回保存。
当我这样做时,活动类别被传递,其中包含从表单添加的详细信息,但项目列表为空。
.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; }
}
}
控制器
[HttpPost]
public ActionResult SaveItems([FromForm] CategoryModel activeCategoryModel, [FromForm] List<ItemModel> items, [FromQuery] string activecategory, [FromQuery] string nextcategory)
{
...
}
查看
@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>
我一直将 IFormCollection
作为参数包含在控制器的 SaveItems 中,这表明项目正在以我认为可行的格式传递,但模型只显示输入的值和项目列表为空。
如何从视图中填充活动类别模型中的项目列表?
Items
in CategoryModel
应该是 属性 而不是归档,所以你的 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; }
}
我有一个 CategoryModel
,其中包含 ItemModel
的列表项以及一些其他字符串等
我正在尝试将列表从视图传递到控制器。视图上的模型是 ABCModel
,它包含类型 CategoryModel
的 'Categories' 列表。在视图中,我只希望 post 'active' 类别返回保存。
当我这样做时,活动类别被传递,其中包含从表单添加的详细信息,但项目列表为空。
.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; }
}
}
控制器
[HttpPost]
public ActionResult SaveItems([FromForm] CategoryModel activeCategoryModel, [FromForm] List<ItemModel> items, [FromQuery] string activecategory, [FromQuery] string nextcategory)
{
...
}
查看
@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>
我一直将 IFormCollection
作为参数包含在控制器的 SaveItems 中,这表明项目正在以我认为可行的格式传递,但模型只显示输入的值和项目列表为空。
如何从视图中填充活动类别模型中的项目列表?
Items
in CategoryModel
应该是 属性 而不是归档,所以你的 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; }
}