我正在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
属性,但是每当我从VueJS应用程序为此字段传递null
值时,都会得到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();
}