我正在尝试创建一个订单页面,如果客户要租用产品,可以在该复选框上打勾。 我的ViewModel包含一个OrderId和一个IList of Articles,其中包含ArticleId和其他参数。 我正在尝试将OrderId和所有ArticleId从打勾的复选框返回到我的控制器。 OrderId可以毫无问题地到达控制器,但是IList of Articles始终返回null。
这是视图:
@model ViewModelOrder
@{
ViewData["Title"] = "OrderCreateChooseArticles";
}
<h1>Choose products</h1>
<form method="post" asp-controller="Order" asp-action="OrderConfirmation">
<button type="submit" class="btn btn-success">Create order</button>
<input type="text" type="hidden" asp-for="orderid" class="form-control" id="OrderId" readonly="readonly">
<table class="table table-striped" style="margin-top: 20px">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Manufacturer</th>
<th scope="col">Publisher</th>
<th scope="col">Rental Days</th>
<th scope="col">Available</th>
<th scope="col">Ckeck</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach (var productResult in Model.products)
{
<partial name="ProductPartial" model="productResult" />
}
</tbody>
</table>
<button type="submit" class="btn btn-success">Create order</button>
</form>
这是部分视图 ProductPartial :
@model VivesRental.Repository.Results.ProductResult
<tr>
<td>@Model.Id</td>
<td>@Model.Name</td>
<td>@Model.Description</td>
<td>@Model.Manufacturer</td>
<td>@Model.Publisher</td>
<td>@Model.RentalExpiresAfterDays</td>
<td>
@if (@Model.NumberOfAvailableArticles >= 5)
{
<span class="badge badge-success badge-pill"> @Model.NumberOfAvailableArticles articles available</span>
}
else if (@Model.NumberOfAvailableArticles >= 1 && @Model.NumberOfAvailableArticles <= 4)
{
<span class="badge badge-warning badge-pill"> @Model.NumberOfAvailableArticles articles available</span>
}
else
{
<span class="badge badge-danger badge-pill"> @Model.NumberOfAvailableArticles articles available</span>
}
</td>
<td>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="@Model.Id" id="defaultCheck">
</div>
</td>
</tr>
这是在控制器中调用的方法:
public IActionResult OrderConfirmation(ViewModelOrder vm)
{
foreach (var product in vm.products)
{
_orderLineService.Rent(vm.orderid, product.Id);
}
return View();
}
这是ViewModel:
public class ViewModelOrder
{
public Guid orderid { get; set; }
public IList<ProductResult> products { get; set; }
}