作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有 Fluent Validation 的 Net 6,并且我有一个带有以下字段的表单:
<form method="post" asp-controller="Product" asp-action="Create" asp-antiforgery="true" autocomplete="off">
<label asp-for="Description">Description</label>
<input asp-for="Description" type="text">
<span asp-validation-for="Description" class="error"></span>
...
<button class="submit" name="button">Create</button>
</form>
ProductModel
是:
public class ProductModel {
public String Description { get; set; }
// ...
}
ProductModel Fluent Validator 是:
公共(public)类 ModelValidator : AbstractValidator {
public ModelValidator() {
RuleFor(x => x.Description)
.Length(0, 200).WithMessage("Do not exceed 200 characters");
// ...
}
}
当我提交表单时,如果我让它为空,我会收到描述错误:
The Description field is required.
但我的验证器不需要描述。
这发生在所有领域。未填写时,我会收到类似的错误。
我错过了什么?
最佳答案
做了一点挖掘,这似乎是由于与模型验证更改有关的问题;特别是在 ASP Net 6 中。我找到了一个可以比我更好地解释它的文档链接,但我也会给出一个代码实现:Microsoft docs
builder.Services.AddControllers(
options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
//Removes the required attribute for non-nullable reference types.
希望这会有所帮助,我直接从 MS 文档中获取了此代码,所以如果它不能解决您的问题,可能还有其他原因。
关于asp.net-core - 表单字段是必需的,即使未如此定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72060349/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!