ASP.NET Core 2.1中的远程验证值未传递给控制器

时间:2018-10-30 09:54:45

标签: c# asp.net validation asp.net-core asp.net-core-mvc

我正尝试通过以下文档使用远程验证: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参数为空。

非常感谢您的帮助。

0 个答案:

没有答案