将TryUpdateModelAsync与IEnumerable Collection一起使用

时间:2017-10-18 06:51:23

标签: c# entity-framework asp.net-core

型号:

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集合?

或者有更好的方法来更新吗?

0 个答案:

没有答案