2011-01-06 113 views
0

我想在ASP.NET MVC 3中獲得遠程驗證,但由於某種原因驗證永遠不會被解僱。我從控制器返回json,並在FireFox中要求我下載這些文件。不知道這裏發生了什麼事。這裏是我的代碼:ASP.NET MVC中的遠程驗證3

@using(Html.BeginForm(new {Action = "ValidateUserName"})) { 

<text> Enter UserName: </text> @Html.TextBoxFor(x => x.UserName) 

<input type="submit" value="Login" /> 



} 

這裏是RegistrationViewModel:

public class RegistrationViewModel 
    { 
     [Required(ErrorMessage = "UserName is required!")] 
     [Remote("ValidateUserName","Home",ErrorMessage ="UserName already taken!")] 
     public string UserName { get; set; } 
    } 

這裏是HomeController的:

public ActionResult ValidateUserName(RegistrationViewModel registrationViewModel) 
     { 
      return Json(!registrationViewModel.UserName.Equals("test"),JsonRequestBehavior.AllowGet); 

     } 

回答

2

一對夫婦的事情要考慮:

1)在你看來,你應該引用jQuery驗證和不顯眼的JavaScript庫:


    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

2)同樣在你看來,你應該有一個Html.ValidationMessageFor(m => m。 屬性):


    @Html.ValidationMessageFor(x => x.UserName) 

3)最後,確保你在你的web.config文件中兩個的AppSettings,使客戶端驗證。


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