英文:
TryUpdateModelAsync() fails without saying why
问题
我有一个TryUpdateModelAsync
的调用,如果失败,只会返回false。不太明显为什么失败。如何获取更多信息?
if (await TryUpdateModelAsync<ComputerFile>(
computerFileToUpdate,
"computerfile",
f => f.FileName, f => f.ContentDescription, f => f.SourceItemID, f => f.FileTypeID,
f => f.CreatedOnDate, f => f.CreatedByID, f => f.ModifiedOnDate, f => f.ModifiedByID))
{
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return Page();
英文:
I have a TryUpdateModelAsync
call that, upon failing, simply returns false. It's not obvious why it's failing. How can I get more information?
if (await TryUpdateModelAsync<ComputerFile>(
computerFileToUpdate,
"computerfile",
f => f.FileName, f => f.ContentDescription, f => f.SourceItemID, f => f.FileTypeID,
f => f.CreatedOnDate, f => f.CreatedByID, f => f.ModifiedOnDate, f => f.ModifiedByID))
{
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return Page();
答案1
得分: 5
希望这有所帮助,您只需使用以下代码:
ModelState.IsValid
通过使用这个,您将完全了解到底是什么确切的错误。
var validationErrors = ModelState.Values.Where(E => E.Errors.Count > 0)
.SelectMany(E => E.Errors)
.Select(E => E.ErrorMessage)
.ToList();
英文:
I hope this helps you can just use
ModelState.IsValid and by using this you will get the perfect idea about what are the exact errors
var validationErrors = ModelState.Values.Where(E => E.Errors.Count > 0)
.SelectMany(E => E.Errors)
.Select(E => E.ErrorMessage)
.ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论