1

我目前正在设置使用MVC3进行远程验证的过程,以便在用户选择的用户名已存在的情况下提醒用户。一切从最重要的部分开始正确设置并正确工作,错误信息不被显示。如果我提交表单,则会在页面刷新时显示错误消息,并添加相关的模型状态错误。MVC3远程验证

有没有办法用Json结果刷新模型验证摘要?

+1

这应该工作。你肯定有不引人注目的验证工作吗?您的控制器操作是否受到影响,并且是否返回了Json结果? –

回答

1

示例代码:

Web.config文件必须包含这样的:

<add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

的看法是这样的:

@model Dommer.Domain.RegisterModel 
@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create New User</h2> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") 
    <div> 
     <fieldset> 
      <legend>Account Information</legend> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.UserName) 
      </div> 
      <div class="editor-field"> 
       @Html.TextBoxFor(m => m.UserName) 
       @Html.ValidationMessageFor(m => m.UserName) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.Email) 
      </div> 
      <div class="editor-field"> 
       @Html.TextBoxFor(m => m.Email) 
       @Html.ValidationMessageFor(m => m.Email) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.Password) 
      </div> 
      <div class="editor-field"> 
       @Html.PasswordFor(m => m.Password) 
       @Html.ValidationMessageFor(m => m.Password) 
      </div> 

      <div class="editor-label"> 
       @Html.LabelFor(m => m.ConfirmPassword) 
      </div> 
      <div class="editor-field"> 
       @Html.PasswordFor(m => m.ConfirmPassword) 
       @Html.ValidationMessageFor(m => m.ConfirmPassword) 
      </div> 
     </fieldset> 
     <input type="submit" value="Create User" /> 
    </div> 
} 

这是该模型被传递到View:

public class RegisterModel 
{ 
    [Required] 
    [RegularExpression(@"(\S)+", ErrorMessage = "Username cannot contain spaces.")] 
    [Remote("CheckUserName", HttpMethod="POST")] 
    [Display(Name = "Username")] 
    public string UserName { get; set; } 

    [Required] 
    [Remote("CheckEmailAddress", ErrorMessage="{0} already has an account, please enter a different email address.", HttpMethod="POST")] 
    [DataAnnotationsExtensions.Email(ErrorMessage="{0} is not a valid email address.")] 
    [Display(Name = "Email address")] 
    public string Email { get; set; } 

    [Required] 
    [ValidatePasswordLength] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 
} 

这是在控制器

[HttpPost] 
public JsonResult CheckUserName(string userName, Guid? userId = null) 
{ 
    if (userName != null || userName.Length > 2) 
    { 
     var users = Membership.FindUsersByName(userName); 
     if (users.Count == 0) 
     { 
       return Json(true); 
     } 
     else 
     { 
      if ((users[userName].ProviderUserKey as Guid?) == userId) 
      { 
       return Json(true); 
      } 
      else 
      { 
       string suggestedUID = String.Format(CultureInfo.InvariantCulture, "{0} is not available.", userName); 
       for (int i = 1; i < 100; i++) 
       { 
        string altCandidate = userName + i.ToString(); 
        if (Membership.FindUsersByName(altCandidate).Count == 0) 
        { 
         suggestedUID = String.Format(CultureInfo.InvariantCulture, "{0} is not available. Try {1}.", userName, altCandidate); 
         break; 
        } 
       } 
       return Json(suggestedUID); 
      } 
     } 
    } 
    else 
    { 
     return Json(true); 
    } 
} 

这将尝试附加号码的名字,直到找到一个可用的用户名,并会做这样的事情没有做完整的回发:

enter image description here

+0

不错的解决方案,但仍然不适合我。该请求正在被处理并传回,盒子甚至以选定的方式被设计。唯一的问题是没有显示文本。 JSON请求的结果是否应该对验证摘要进行“刷新”?我真的很困惑,我已经很久了。 –

+1

我有这个代码工作(我甚至现在只是试了一下这一分钟),所以一定会有不同的事情发生...... –

+0

也许是一个愚蠢的问题,但你实际上是否显示验证信息?查看我的回答更新(视图代码片段)... –