I have some problem with create page with list in the same page, I know this is old question, but all example I sow not working in my case, if someone can help, i put some what I need. I have a page where i insert all my invoices and this invoices have many product, so I try to put all things in the same page. Look bellow
My view model
<code>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; }
}
</code>
<code>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; }
}
</code>
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; }
}
My controller:
<code> [HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PurchaseVM purchaseVM)
{
if (ModelState.IsValid)
{
_unitOfWork.Purchase.Add(purchaseVM.purchases);
_unitOfWork.Save();
TempData["success"]= "Product added successfully.";
//return View(purchaseVM);
return PartialView("_CreatePartialView", _unitOfWork.Purchase.GetAll());
}
}
</code>
<code> [HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PurchaseVM purchaseVM)
{
if (ModelState.IsValid)
{
_unitOfWork.Purchase.Add(purchaseVM.purchases);
_unitOfWork.Save();
TempData["success"]= "Product added successfully.";
//return View(purchaseVM);
return PartialView("_CreatePartialView", _unitOfWork.Purchase.GetAll());
}
}
</code>
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(PurchaseVM purchaseVM)
{
if (ModelState.IsValid)
{
_unitOfWork.Purchase.Add(purchaseVM.purchases);
_unitOfWork.Save();
TempData["success"]= "Product added successfully.";
//return View(purchaseVM);
return PartialView("_CreatePartialView", _unitOfWork.Purchase.GetAll());
}
}
My partial view:
<code>@model List<Purchases>
@foreach (var purchase in Model)
{
<tr>
<td>@purchase.PurchaseId</td>
<td>@purchase.PurcahseDate</td>
<td>@purchase.Suppliers.SupplierName</td>
<td>@purchase.Suppliers.SupplierPhone</td>
<td>
<!-- Action buttons -->
</td>
</tr>
}
</code>
<code>@model List<Purchases>
@foreach (var purchase in Model)
{
<tr>
<td>@purchase.PurchaseId</td>
<td>@purchase.PurcahseDate</td>
<td>@purchase.Suppliers.SupplierName</td>
<td>@purchase.Suppliers.SupplierPhone</td>
<td>
<!-- Action buttons -->
</td>
</tr>
}
</code>
@model List<Purchases>
@foreach (var purchase in Model)
{
<tr>
<td>@purchase.PurchaseId</td>
<td>@purchase.PurcahseDate</td>
<td>@purchase.Suppliers.SupplierName</td>
<td>@purchase.Suppliers.SupplierPhone</td>
<td>
<!-- Action buttons -->
</td>
</tr>
}
1