型号:
public class InvoiceCategory
{
[Key]
public int InvoiceCategoryID { get; set; }
[Required]
public string CategoryName { get; set; }
}
控制器编辑方法:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(IEnumerable<InvoiceCategory> invoiceCategories)
{
if (ModelState.IsValid)
{
try
{
var categoryToUpdate = await _context.InvoiceCategory.ToListAsync();
if (await TryUpdateModelAsync<InvoiceCategory>(
categoryToUpdate,
"",
i => i.InvoiceCategories)) /// <-- This is incorrect
{
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
...
}
}
return View();
}
如何使用TryUpdateModelAsync更新IEnumerable集合?
或者有更好的方法来更新吗?