[英]asp.net core unobtrusive validation fire on a non required field
I have a model with 2 fields and both are non required(no [Required] tag is used). 我有一个带有2个字段的模型,并且两个字段都不是必需的(不使用[Required]标签)。
On the razor page I have unobtrusive and jquery validation js files included. 在剃须刀页面上,我包含了不引人注目的和jquery验证js文件。 When I do not fill any value and post the form I get an error(client side) that ask for the last field in my form is required. 当我不填写任何值并发布表单时,出现错误(客户端),要求输入表单中的最后一个字段。
I have searched but have not found similar issue, as there is no required tag in model/viewmodel then why this is required on client side. 我已经搜索过但没有发现类似的问题,因为在model / viewmodel中没有必需的标记,那么为什么在客户端需要它。
[Update 1 : code added] [更新1:已添加代码]
Model: 模型:
public class AppUser: IdentityUser
{
public string Name { get; set; }
public int Deposit { get; set; }
}
View: 视图:
<form method="post">
<div class="form-group">
<label asp-for="@Model.AppUser.Deposit" class="control-label"></label>
<input asp-for="@Model.AppUser.Deposit" type="text" class="form-control" />
<span asp-validation-for="@Model.AppUser.Deposit" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.AppUser.Email" class="control-label"></label>
<input asp-for="@Model.AppUser.Email" type="text" class="form-control" />
<span asp-validation-for="@Model.AppUser.Email" class="text-danger"></span>
</div>
</form>
Controller: 控制器:
public class SomeModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<AppUser> _userManager;
[BindProperty]
public AppUser AppUser { get; set; }
public SomeModel(ApplicationDbContext context, UserManager<AppUser> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<IActionResult> OnGetAsync()
{
//..some action
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
//..some action
return Page();
}
}
默认情况下,如果想要非空类型(例如int
, double
, byte
,...)为可选,则必须使用可空类型,例如int?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.