c# - 字符串字段是必需的。即使你在 Asp.Net Core 中也没有必需的属性?
问题描述
我正在 linux(pop os) 中构建一个简单的 Asp.Net Core 应用程序。我正在使用 VueJs + Aps.Net Core 3.1.101 我正在尝试对我的应用程序进行 POST 调用,我的模型如下所示:
public class AddConfigurationContextValueApiRequest
{
public int ContextId { get; set; }
[Required(ErrorMessage = "Value is required to continue")]
[StringLength(500, ErrorMessage = "Value can not be longer than 500 characters")]
public string Value { get; set; }
[StringLength(500, ErrorMessage = "Display name can not be longer than 500 characters")]
public string DisplayName { get; set; }
}
如您所见,该字段没有Required
属性DisplayName
,但是每当我null
从 VueJS 应用程序为该字段传递一个值时,我都会得到The DisplayName field is required.
.
我试图弄清楚为什么 AspNet Core 会为此抱怨,因为Required
这样的字段没有属性!
有人知道这是故意的吗?我试图删除该StringLength
属性,但它仍然触发了必需的属性。
我的动作很简单:
[HttpPost(UrlPath + "addConfigurationContextValue")]
public async Task AddConfigurationContextValue([FromBody]AddConfigurationContextValueApiRequest request)
{
using var unitOfWork = _unitOfWorkProvider.GetOrCreate();
if (!ModelState.IsValid)
{
//Here it throws because ModelState is invalid
throw new BadRequestException(ModelState.GetErrors());
}
//do stuff
await unitOfWork.CommitAndCheckAsync();
}
解决方案
推荐阅读
- php - 返回选择字段 laravel Nova 中的所有表行
- javascript - Firebase + Socket.io。这有意义吗?
- msal - 在 iOS 单元测试 XCTestCase 中创建 MSALPublicClientApplication 对象的错误
- json - 想要创建 Visual Studio Snippets 但它给出了错误
- ember.js - Ember 中有没有办法根据 ember8013 之类的 div id 获取组件/视图/路由?
- matlab - 像素到区域的转换无法在 Matlab im2bw 中正常工作
- javascript - 没有 iFrame 的 Google 地图方向按钮
- java - 是否需要手动销毁和关闭 java.lang.Process 的打开流?
- html - HTML 媒体查询最小值和最大值
- node.js - 返回nodejs和mongodb中嵌套数组的一项