我是ASP的初学者。NET和我想在下面的问题你的帮助,我正在开发一个屏幕,我在那里购买,我有队列,使释放的发票,这个发票有几个项目,但我想目前在一个列表中的索引页,使用foreach这个项目,但当我添加foreach我有一个错误,我会离开这里的代码。
我的模特
namespace SysComm.Models
{
public class Purchases
{
[Key]
public int PurchaseId { get; set; }
[Required]
public DateOnly PurcahseDate { get; set; }
[Required]
[DisplayName("Invoice Nº.")]
[StringLength(100)]
public string PurchaseInvoice { get; set; }
[DisplayName("Supplier")]
public int SupplierId { get; set; }
[ForeignKey("SupplierId")]
[ValidateNever]
[Required]
public Suppliers Suppliers { get; set; }
[Required]
public DateOnly PurchaseDateDue { get; set; }
[DisplayName("BarCode")]
public string? PurchaseBarCode { get; set; }
[DisplayName("Group Name")]
public int GroupId { get; set; }
[ForeignKey("GroupId")]
[ValidateNever]
[Required]
public Groups Groups { get; set; }
[DisplayName("Product Name")]
public int ProductId { get; set; }
[ForeignKey("ProductId")]
[ValidateNever]
[Required]
public Products Products { get; set;}
[Required]
[DisplayName("Quantity")]
[Column(TypeName ="decimal(18,4)")]
public decimal PurchaseQuantity { get; set; }
[Required]
[DisplayName("Purchase Price")]
[Column(TypeName = "decimal(18,2)")]
public decimal PurchaseUnityCost { get; set; }
[Required]
[DisplayName("VAT Percentual")]
[Column(TypeName ="decimal(18,4)")]
public decimal PurchaseVatPerc { get; set; }
[Required]
[DisplayName("VAT Value")]
[Column(TypeName ="decimal(18,m2)")]
public decimal PurchaseVatValue { get; set;}
[Required]
[DisplayName("Total Product")]
[Column(TypeName ="decimal(18,2)")]
public decimal PurchaseTotal { get; set; }
}
}
我的控制器:
public class PurchaseController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public PurchaseController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
List<Purchases> objPurchaseList = _unitOfWork.Purchase.GetAll().ToList();
return View(objPurchaseList);
}
//GET
public IActionResult Create()
{
IEnumerable<SelectListItem> GroupList = _unitOfWork.Group
.GetAll().Select(u => new SelectListItem
{
Text = u.GroupName,
Value = u.GroupId.ToString()
});
ViewBag.GroupList = GroupList;
IEnumerable<SelectListItem> SupplierList = _unitOfWork.Supplier
.GetAll().Select(u => new SelectListItem
{
Text = u.SupplierName,
Value = u.SupplierId.ToString()
});
ViewBag.SupplierList = SupplierList;
IEnumerable<SelectListItem> ProductList = _unitOfWork.Product
.GetAll().Select(u => new SelectListItem
{
Text = u.ProductName,
Value = u.ProductId.ToString()
});
ViewBag.ProductList = ProductList;
return View();
}
}
我的观点
@model Purchases
<form method="post">
<div class="border p-3 mt-4 row">
<div class="row pb-2">
<h2 class="text-primary">Create New Purchase</h2>
<hr />
</div>
<div class="mb-3 col-2">
<label asp-for="PurcahseDate"></label>
<input asp-for="PurcahseDate" class="form-control" autofocus="autofocus" style="text-transform:capitalize" />
<span asp-validation-for="PurcahseDate" class="text-danger"></span>
</div>
<div class="mb-3 col-8">
<label asp-for="SupplierId"></label>
<select asp-for="SupplierId" asp-items="ViewBag.SupplierList" class="form-select">
<option disabled selected>--Select Supplier--</option>
</select>
<span asp-validation-for="SupplierId" class="text-danger"></span>
</div>
<div class="mb-3 col-2">
<label asp-for="PurchaseDateDue"></label>
<input asp-for="PurchaseDateDue" class="form-control" style="text-transform:uppercase" />
<span asp-validation-for="PurchaseDateDue" class="text-danger"></span>
</div>
<div class="mb-3 col-4">
<label asp-for="PurchaseBarCode"></label>
<input asp-for="PurchaseBarCode" class="form-control" style="text-transform:capitalize" />
<span asp-validation-for="PurchaseBarCode" class="text-danger"></span>
</div>
<div class="mb-3 col-8">
<label asp-for="GroupId"></label>
<select asp-for="GroupId" asp-items="ViewBag.GroupList" class="form-select">
<option disabled selected>--Select Group--</option>
</select>
<span asp-validation-for="GroupId" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="ProductId"></label>
<select asp-for="ProductId" asp-items="ViewBag.ProductList" class="form-select">
<option disabled selected>--Select Prodcut--</option>
</select>
<span asp-validation-for="ProductId" class="text-danger"></span>
</div>
<div class="mb-3 col-2">
<label asp-for="PurchaseQuantity"></label>
<input asp-for="PurchaseQuantity" class="form-control"/>
<span asp-validation-for="PurchaseQuantity" class="text-danger"></span>
</div>
<div class="mb-3 col-2">
<label asp-for="PurchaseUnityCost"></label>
<input asp-for="PurchaseUnityCost" class="form-control"/>
<span asp-validation-for="PurchaseUnityCost" class="text-danger"></span>
</div>
<div class="mb-3 col-2">
<label asp-for="PurchaseVatPerc"></label>
<input asp-for="PurchaseVatPerc" class="form-control"/>
<span asp-validation-for="PurchaseVatPerc" class="text-danger"></span>
</div>
<div class="mb-3 col-2">
<label asp-for="PurchaseVatValue"></label>
<input asp-for="PurchaseVatValue" class="form-control"/>
<span asp-validation-for="PurchaseVatValue" class="text-danger"></span>
</div>
<div class="mb-3 col-4">
<label asp-for="PurchaseTotal"></label>
<input asp-for="PurchaseTotal" class="form-control" />
<span asp-validation-for="PurchaseTotal" class="text-danger"></span>
</div>
<div class="mb-3 col-6 col-md-3">
<button type="submit" class="btn btn-primary form-control">Create</button>
</div>
<div class="mb-3 col-6 col-md-3">
<a asp-controller="Purchase" asp-action="Index" class="btn btn-secondary form-control">
Back to List
</a>
</div>
</div>
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
Date
</th>
<th>
Invoice Nº
</th>
<th>
Supplier Name
</th>
<th>
Total Invoice
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
@foreach (var obj in Model)* => HERE IS THE ERROR*
{
<tr>
<td width="10%">
@obj.PurcahseDate
</td>
<td width="100%">
@obj.PurchaseInvoice
</td>
<td width="40%">
@obj.SupplierId
</td>
<td width="20%">
@Model.Sum(u=> u.PurchaseTotal)
</td>
<td width="20%">
<div class="w-75 btn-group" role="group">
<a asp-controller="Purchase" asp-action="Edit" style="width:120px" asp-route-id="@obj.PurchaseId"
class="btn btn-primary mx-2"> <i class="bi bi-pencil-square"></i> Edit</a>
<a asp-controller="Purchase" asp-action="Delete" style="width:125px" asp-route-id="@obj.PurchaseId"
class="btn btn-danger mx-2"> <i class="bi bi-trash-fill"></i> Delete</a>
</div>
</td>
</tr>
}
</tbody>
</table>
</form>
@section Scripts {
@{
<partial name="_ValidationScriptsPartial" />
}
}
错误发生在foreach循环中。有人能帮忙吗?谢谢大家。
1条答案
按热度按时间nkoocmlb1#
foreach
需要迭代收集。因此,例如,有必要将数据模型声明为IEnumerable<Purchases>
或者作为列表:
顺便说一句,
public IActionResult Index()
的代码正确地准备了数据模型,并在渲染时将列表传递给视图。因此,您只需要在@model
声明中正确地声明数据模型。