TryUpdateModelAsync()在不提供失败原因的情况下失败。

huangapple go评论104阅读模式
英文:

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&lt;ComputerFile&gt;(
    computerFileToUpdate,
    &quot;computerfile&quot;,
    f =&gt; f.FileName, f =&gt; f.ContentDescription, f =&gt; f.SourceItemID, f =&gt; f.FileTypeID,
    f =&gt; f.CreatedOnDate, f =&gt; f.CreatedByID, f =&gt; f.ModifiedOnDate, f =&gt; f.ModifiedByID))
{
    await _context.SaveChangesAsync();
    return RedirectToPage(&quot;./Index&quot;);
}

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 =&gt; E.Errors.Count &gt; 0)
.SelectMany(E =&gt; E.Errors)
.Select(E =&gt; E.ErrorMessage)
.ToList(); 

huangapple
  • 本文由 发表于 2020年1月6日 21:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定