显示错误"电子邮件已存在"在登记表格

时间:2014-08-05 21:06:14

标签: asp.net-mvc json validation modelstate server-error

如何在注册表单中显示错误“电子邮件已存在”?

我尝试将错误添加到模型状态 - 但不显示错误。

对于检查电子邮件的行为,我有返回JsonResult的方法。也许这里有一个问题,为什么错误没有显示?

        [AllowAnonymous]
    public JsonResult EmailValidation(string email)
    {
        if (ModelState.IsValid)
        {
            var user = _service.FindByEmail(email);
            if (user == null)
            {
                return Json(new {result = true, message = "Email is validated"}, JsonRequestBehavior.AllowGet);
            }
            ModelState.AddModelError(string.Empty, "Your Email already exists");
            return Json(new {result = false, message = "Email already exists"}, JsonRequestBehavior.AllowGet);
        }
        return Json(new {result = false, message = "Input Email"}, JsonRequestBehavior.AllowGet);
    }

<form id="emailValidationForm">
            @Html.ValidationSummary(true)
            <div class="form-group text-center">
                <p><button type="button" class="btn btn-facebook" title="Join with the Facebook"><i class="fa fa-facebook"></i>&nbsp;&nbsp;Join with the Facebook</button></p>
                <p><button type="button" class="btn btn-linkedin" title="Join with the LinkedIn"><i class="fa fa-linkedin"></i>&nbsp;&nbsp;Join with the LinkedIn</button></p>
                <p><button type="button" class="btn btn-googleplus" title="Join with the Google+"><i class="fa fa-google-plus"></i>&nbsp;&nbsp;Join with the Google+</button></p>
                <p>or</p>
                <div class="input-group margin-bottom-5">
                    <span class="input-group-addon"><i class="fa fa-user"></i></span>
                    @Html.TextBoxFor(m => m.Email, new {@class="form-control", @placeholder="Enter your Email",@data_toggle="tooltip"})
                    @*<input type="text" name="Email" placeholder="Enter your Email" class="form-control" data-toggle="tooltip">*@
                </div>
                @Html.ValidationMessageFor(m => m.Email, null, new { @class = "text-danger" })
            </div>
            <div class="text-right">
                <button type="submit" class="btn btn-primary" title="Validate email">Join</button>    
            </div>
        </form>

3 个答案:

答案 0 :(得分:0)

我猜这是因为你正在返回一个JSON结果,但显示错误消息的代码是使用razor。也许你可以返回视图。

答案 1 :(得分:0)

我使用此答案https://stackoverflow.com/a/2759898/2167382解决了我的问题 我只是将我的观点作为Json返回,它运行正常。

public string RenderRazorViewToString(string viewName, object model)
{
  ViewData.Model = model;
  using (var sw = new StringWriter())
  {
    var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                             viewName);
    var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                 ViewData, TempData, sw);
    viewResult.View.Render(viewContext, sw);
    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
    return sw.GetStringBuilder().ToString();
  }
}

答案 2 :(得分:0)

如果我们谈论MVC 5,最好的解决方案就在这里:How to check if user already exists on client-side in ASP.NET MVC 5?

简单,清晰,聪明!