无法使用 asp.net 核心 2.2 中的标签助手将对象列表从视图发送到控制器
Can not send list of objects from view to controller using tag helper in asp.net core 2.2
我有以下视图,我想将对象列表从视图发送到控制器,我使用 asp-for 标签助手进行数据绑定,但控制器中的操作接收到 null
@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>
这是我的控制器,它接收到 null
[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>
结果:
另一种方式:
@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,"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>
我有以下视图,我想将对象列表从视图发送到控制器,我使用 asp-for 标签助手进行数据绑定,但控制器中的操作接收到 null
@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>
这是我的控制器,它接收到 null
[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>
结果:
另一种方式:
@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,"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>