我正尝试通过以下文档使用远程验证:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.1#remote-validation
我的方法如下:
[AcceptVerbs("Get", "Post")]
public IActionResult UniqueName(string name) //name comes in as null
{
if (_categoryRepository.DoesCategoryNameExist(name))
return Json($"{name} already exists.");
return Json(true); //valid
}
验证属性的用法如下:
public class Category
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Name is required and must not be empty.")]
[StringLength(200, ErrorMessage = "Name should not exceed 200 characters.")]
[Remote(action: "UniqueName", controller: "Category")]
public string Name { get; set; }
}
视图如下:
<div class="form-group">
<input asp-for="Category.Name" class="form-control frmInput" id="categoryName" placeholder="Name">
<span asp-validation-for="Category.Name" class="text-danger"></span>
</div>
触发了远程验证,但是UniqueName方法中的name参数为空。
非常感谢您的帮助。