繁体   English   中英

使用.NET Core执行远程验证

[英]Perform Remote Validation with .NET core

我认为这很简单,但显然并非如此,原因是目前我不清楚。

我正在尝试验证要在我的网站上注册的用户的电子邮件地址。

为此,我使用[Remote]属性从另一个控制器调用方法并进行检查。

除了此方法的输入参数始终为null以外,因此它不会接收用户输入的电子邮件地址。

我看到变量的名称应该与我的模型的名称相同,而且确实是这种情况(至少我认为?!)。

你有想法吗?

直接在RegistrationController中建模

public class InputModel
{
    [Remote("ValidateEmail", "Home", ErrorMessage = "This email his already used.")]
    [Required]
    [EmailAddress]
    [Display(Name = "Email *")]
    public string Email { get; set; }
}



**Home Controller where the method is**



public async Task<IActionResult> ValidateEmail(string Email)
{
    var user = await _userManager.FindByNameAsync(Email);

    bool result;

    if (user.UserName == null)
    {
        result = true;
    }
    else
    {
        result = false;
    }
    return Json(result);
}

视图

            <div class="form-check-inline col-12">
                <div class="col-4">
                    <label asp-for="Input.Email"></label>
                </div>
                <input asp-for="Input.Email" class="form-control" />
            </div>
            <div style="margin-left:33%">
                <span asp-validation-for="Input.Email" class="text-danger"></span>
            </div>

对于remote validation ,它将附加Input.Email作为查询字符串,但是您将参数与(string Email)绑定,该绑定将无法绑定。

尝试通过FromQuery绑定FromQuery

public async Task<IActionResult> ValidateEmail([FromQuery(Name = "Input.Email")]string Email)
{
    bool result = false;
    return Json(result);
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM