請教大家以下兩個errMsg裏面寫的是什麼意思,謝謝
1.第1個
if (ModelState.IsValid)
{
}
{
var _errMsg = ModelState.Values.Where(x => x.Errors.Count > 0)?.FirstOrDefault()?.Errors.Select(y => y.ErrorMessage).FirstOrDefault();
return Content(HttpStatusCode.BadRequest, _errMsg);
}
2.第2個
if (ModelState.IsValid)
{
}
else
{
var _errMsg = string.Join("; ", ModelState.Values
.SelectMany(x => x.Errors)
.Where(x => !string.IsNullOrEmpty(x.ErrorMessage))
.Select(x => x.ErrorMessage));
return Content(HttpStatusCode.BadRequest, _errMsg);
}
驗證分為前後端
一般在前端可能就是畫面一些js驗證
而到後端也就是Server-Side驗證
Controller部分 POST action method
可以透過ModelState.IsValid做驗證
Eroor msg一些錯誤或不符資訊
可藉由ViewModel當時Data Annotation定義的去捕獲
.NET Core第28天_ValidationMessageTagHelper和ValidationSummaryTagHelper的使用
https://coolmandiary.blogspot.com/2021/08/net-core28validationmessagetaghelper.html
.NET Core第29天_Model驗證配置準備流程_各種驗證資料註解使用方式_part1(Required,StringLength,MaxLength,MinLength,RegularExpression,Range,Compare)
https://coolmandiary.blogspot.com/2021/08/net-core29modelpart1.html
.NET Core第30天_Model驗證配置準備流程_各種驗證資料註解使用方式_part2(Remote,DataType,EmailAddress,Display,DisplayFormat驗證)
https://coolmandiary.blogspot.com/2021/08/net-core30modelpart2remotedatatypeemail.html