提问人:trentd 提问时间:11/19/2022 更新时间:11/19/2022 访问量:178
在 C# .net6 和 jquery 中使用 CreditCard 数据注释时出错
Error using the CreditCard data annotation in C# .net6 and jquery unobtrusive
问:
尝试在 .net 6 中使用 [CreditCard] 数据注释
我使用一个简单的表单创建了一个新项目,但在验证信用卡时仍然收到错误。
收到 javascript 错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'call'). Exception occurred when checking element drec_CardNumber, check the 'creditcard' method.
at $.validator.check (jquery.validate.js:778:45)
at $.validator.element (jquery.validate.js:492:15)
at $.validator.onfocusout (jquery.validate.js:300:10)
at HTMLInputElement.delegate (jquery.validate.js:423:28)
at HTMLFormElement.dispatch (jquery.min.js:2:43090)
at v.handle (jquery.min.js:2:41074)
at Object.trigger (jquery.min.js:2:71513)
at Object.simulate (jquery.min.js:2:72016)
at HTMLDocument.i (jquery.min.js:2:72305)
这是使用这些版本的 jquery(来自 dotnet new razor)
// Unobtrusive validation support library for jQuery and jQuery Validate
// @version v3.2.11
* jQuery Validation Plugin v1.17.0
Index.cshtml.cs:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace cardtest.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public class DRec {
public int Id { get; set; }
[Display(Name = "Your Name", Prompt = "John Doe")]
[Required(ErrorMessage = "You must enter your name!")]
public string? Name { get; set; }
[CreditCard]
[Display(Name = "Credit Card", Prompt = "Credit Card Number")]
[Required(ErrorMessage = "Please enter a valid Credit Card Number")]
public string? CardNumber { get; set; }
}
[BindProperty]
public DRec drec {get; set;}
public void OnGet()
{
drec = new();
}
public void OnPost()
{
System.Console.WriteLine($"gak {drec.Name} {drec.CardNumber}");
}
}
Index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<form method="post" data-ajax="true" data-ajax-method="post">
<div class="form-floating col-md-12">
<input asp-for="@Model.drec.Name" class="form-control">
<label asp-for="@Model.drec.Name"></label>
<span asp-validation-for="@Model.drec.Name" class="text-danger"></span>
</div>
<div class="form-floating col-md-12">
<input asp-for="@Model.drec.CardNumber" class="form-control">
<label asp-for="@Model.drec.CardNumber"></label>
<span asp-validation-for="@Model.drec.CardNumber" class="text-danger"></span>
</div>
<input type="submit"/>
</form>
@section scripts{
<partial name="_ValidationScriptsPartial" />
}
程序.cs(我添加的只是运行时编译,错误发生在之前和之后)
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// runtime compilation
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
当信用卡号无效时,我应该收到验证错误。但是检查控制台时,我收到一个javascript错误。
答: 暂无答案
评论