我尝试在同一个“创建”页面中列出我放入同一张发票的产品。所以我需要插入我的供应商发票,当然,在这张发票中我有很多产品,我需要在创建这张发票的同一页面中列出,所以一旦我插入产品,它就会出现在我的列表中,我尝试使用部分视图,但我找不到解决方案。
我的控制器
[Area("***")]
public class PurchaseController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public PurchaseController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
IEnumerable<Purchases> objPurchaseList = _unitOfWork.Purchase.GetAll(includeProperties:"Suppliers").ToList();
return View(objPurchaseList);
}
public IActionResult _CreatePartialView(string? invoice)
{
PurchaseVM purchaseVM = new()
{
purchases = new(),
};
if (invoice==null)
{
return RedirectToAction(nameof(Create));
}
List<Purchases> purchaseList = _unitOfWork.Purchase.GetAll().Where(u => u.PurchaseInvoice == invoice).ToList();
return RedirectToAction(nameof(Create));
}
//GET
public IActionResult Create()
{
PurchaseVM purchaseVM = new()
{
purchases = new(),
GroupList = _unitOfWork.Group.GetAll().Select(u => new SelectListItem
{
Text = u.GroupName,
Value = u.GroupId.ToString()
}),
SupplierList = _unitOfWork.Supplier.GetAll().Select(u => new SelectListItem
{
Text = u.SupplierName,
Value = u.SupplierId.ToString()
}),
ProductList = _unitOfWork.Product.GetAll().Select(u => new SelectListItem
{
Text = u.ProductName,
Value = u.ProductId.ToString()
}),
};
return View(purchaseVM);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Purchases obj)
{
if(ModelState.IsValid)
{
_unitOfWork.Purchase.Add(obj);
_unitOfWork.Save();
TempData["success"]= "Product added successfully.";
return View(obj);
}
return View();
}
}
}
My Partial View
@model IEnumerable<Purchases>
@foreach (var obj in Model.GroupBy(u => u.PurchaseInvoice))
{
<tr>
<td width="15%">
@obj.First().PurcahseDate
</td>
<td width="15%">
@obj.First().PurchaseInvoice
</td>
<td width="35%">
@obj.First().Suppliers.SupplierName
</td>
<td width="15%" style="text-align:right">
@obj.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.First().PurchaseInvoice"
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.First().PurchaseInvoice"
class="btn btn-danger mx-2"> <i class="bi bi-trash-fill"></i> Delete</a>
</div>
</td>
</tr>
}
My ViewModel
namespace SysComm.Models.ViewModels
{
public class PurchaseVM
{
public Purchases purchases { get;set; }
public List<Purchases> purchaseList { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> GroupList { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> SupplierList { get; set; }
[ValidateNever]
public IEnumerable<SelectListItem> ProductList { get; set; }
}
}
Part off My View
</div>
<table class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
Id
</th>
<th>
Date
</th>
<th>
Customer Name
</th>
<th>
Customer Phone
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
<partial name="~/Areas/Admin/Views/Purchase/_CreatePartialView.cshtml" />
</tbody>
</table>
</div>
</div>
</div>
我将使用剃须刀作为样本。 您首先需要定义您的局部视图。 例如在共享文件夹中。我们将其命名为“PagerPartial”。
在顶部定义要使用的对象,如下所示:
@model someclass
定义后,您可以在剃刀页面中使用此模型,如下所示:
<b>@Model.SomeProperty</b> item(s) listed
完成部分视图后,您可以在其他剃刀页面中使用它,如下所示:
@model SomeRazorProjectName.Pages.IndexModel
<div>
@await Html.PartialAsync("PagerPartial", Model.SomePropertyWithTypeOfSomeClass)
</div>
请注意,“IndexModel”是我的razor页面的类,“SomePropertyWithTypeOfSomeClass”是我的页面模型中定义的类型“someclass”的属性。
希望这有帮助。