【问题标题】:How To Add A Link Element Along With Validation Message IF Validation Fails in mvc views如果 mvc 视图中的验证失败,如何添加链接元素以及验证消息
【发布时间】:2020-11-08 22:05:30
【问题描述】:

我有要求,如果电子邮件字段验证(如电子邮件)已经在使用时,它会显示验证消息电子邮件现在已经连同此验证消息一起使用我想附加登录链接我该怎么做

<div>
  <input asp-for="Email" type="text" placeholder="Email" class="form-control" />
 </div>
<span asp-validation-for="Email" id="err" class="text-danger"></span>
<a  asp-area="" asp-controller="Account" asp-action="Login" asp-route-aname="indiaRegion">login</a>
Any Help That Grate For Me How Can I append The Link At Time Of email Validation Fail  I have Remote Email Verification For Already In Use 

【问题讨论】:

    标签: asp.net-core model-view-controller


    【解决方案1】:

    尝试使用远程验证并在验证消息中添加&lt;a&gt; 标签。检查以下代码:

    用户视图模型:

        [Remote(action: "VerifyEmail", controller: "Home")]
        public string Email { get; set; }
    

    视图中的代码:

            <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span> 
            </div> 
    

    控制器中的代码:

        [AcceptVerbs("GET", "POST")]
        public IActionResult VerifyEmail(string email)
        { 
            //Generate the login url:
            string loginurl = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, "Identity/Account/Login");
            if (_userService.VerifyEmail(email))
            {
                return Json($"Email {email} is already in use, click the <a href='" + loginurl + "'> here </a> to login.");
            }
    
            return Json(true);
        }
    

    测试截图:

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 2011-10-03
      • 1970-01-01
      • 2015-04-17
      • 2014-11-21
      • 1970-01-01
      • 2017-07-04
      • 2017-10-13
      • 2023-03-22
      相关资源
      最近更新 更多