简体   繁体   English

asp.net核心非强制性验证在非必填字段上触发

[英]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();
}
}

默认情况下,如果想要非空类型(例如intdoublebyte ,...)为可选,则必须使用可空类型,例如int?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC5:在填写表单字段期间不显眼的验证 - ASP.NET MVC5: Unobtrusive validation during the filling out of the form field cshtml文件中的Asp.Net Core电子邮件字段验证 - Asp.Net Core Email Field validation in cshtml file ASP.NET 核心 6 jQuery 验证不起作用 - ASP.NET Core 6 jQuery Validation not working ASP.NET Core 2 Razor页面/实体框架-在帖子上设置必填字段 - ASP.NET Core 2 Razor Pages/Entity Framework - Set required field on post 在后端调用Web API时在ASP.NET MVC 5中进行不显眼的客户端验证 - Unobtrusive Client Validation in ASP.NET MVC 5 when calling web api in the back end 尽管具有相同代码的字段完美运行(在 C# MVC、ASP.Net Core 中),但 DateTime 验证不起作用 - DateTime validation not working despite a field with identical code working perfectly (in C# MVC, ASP.Net Core) ASP.NET CORE 2中的AJAX更新字段 - AJAX Update Field in ASP.NET CORE 2 如何使用 asp.net core 3.1/5 mvc 在 razor 视图中使用 * (基于 [Required] 注释)自动标记必填字段标签? - How can I automatically mark required field labels with a * (based on [Required] annotation) in a razor view using asp.net core 3.1/5 mvc? asp.net mvc 5,验证非必填字段(可选字段) - asp.net mvc 5, validation for not required fields(optional fields) ASP.NET MVC 4在Radio按钮上需要验证客户端 - ASP.NET MVC 4 Required Validation client side on Radio Buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM