2011-04-19 49 views
2

我有一個簡單的模型是默認RegisterModel並在創建Create基於該模型視圖我最終ASP.NET MVC3遠程驗證不輸出錯誤信息

public class RegisterModel 
{ 
    [Required] 
    [Remote("UserNameExists", "Account", "", ErrorMessage = "Username is already taken.")] 
    [Display(Name = "Username (spaces will be stripped, must be at least 6 characters long)")] 
    public string UserName { get; set; } 

    [Required] 
    [Editable(true)] 
    [Display(Name = "First and Last name")] 
    public string Name { get; set; } 

    [Required] 
    [DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")] 
    [Remote("EmailExists", "Account", "", ErrorMessage = "Email is already taken.")] 
    [Display(Name = "Email address")] 
    public string Email { get; set; } 

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

    [DataType(DataType.Password)] 
    [Display(Name = "Verify password")] 
    //[Compare("Password", ErrorMessage = "Password's do not match.")] 
    public string ConfirmPassword { get; set; } 
} 

,並在觀:

<h3> 
    Details</h3> 
@using (Html.BeginForm("GenerateBetaLink", "Account", FormMethod.Post, new { @id = "beta-user" })) 
{ 
    @Html.ValidationSummary(true) 
    <div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.UserName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.UserName) 
      @Html.ValidationMessageFor(model => model.UserName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Name) 
      @Html.ValidationMessageFor(model => model.Name) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Email) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Email) 
      @Html.ValidationMessageFor(model => model.Email) 
     </div> 
     <p> 
      &nbsp;</p> 
     <p> 
      <input type="submit" value="Create Beta User" class="btn-submit" /> 
      <span class="loading"></span> 
     </p> 
    </div> 
} 

Validation Controller

public class ValidationController : Controller 
{ 
    public JsonResult UserNameExists(string UserName) 
    { 
     OnlineServicesRepository db = new OnlineServicesRepository(); 

     var user = db.FindUserByUsername(UserName.Trim()); 
     return user == null ? 
      Json(true, JsonRequestBehavior.AllowGet) : 
      Json(string.Format("{0} is not available.", UserName), 
       JsonRequestBehavior.AllowGet); 
    } 

    public JsonResult EmailExists(string Email) 
    { 
     OnlineServicesRepository db = new OnlineServicesRepository(); 

     var user = db.FindUserByEmail(Email.Trim()); 
     return user != null ? 
      Json(true, JsonRequestBehavior.AllowGet) : 
      Json(string.Format("{0} is not available.", Email), 
       JsonRequestBehavior.AllowGet); 
    } 
} 

我的問題是是遠程驗證不火,但不寫任何東西到錯誤消息,因爲它應該加上,jQuery的方法.valid()不斷告訴我形式是有效

http://www.balexandre.com/temp/2011-04-19_0900.png

我缺少什麼在這裏嗎?

MSDN article顯示了相同的代碼(在可下載的文件)

回答

7

對我來說,以下的罰款:

型號:

public class RegisterModel 
{ 
    [Required] 
    [DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")] 
    [Remote("EmailExists", "Home", "")] 
    [Display(Name = "Email address")] 
    public string Email { get; set; } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(RegisterModel model) 
    { 
     return View(model); 
    } 

    public ActionResult EmailExists(string email) 
    { 
     if ((email ?? string.Empty).Contains("foo")) 
     { 
      return Json(email + " is not available", JsonRequestBehavior.AllowGet); 
     } 

     return Json(true, JsonRequestBehavior.AllowGet); 
    } 
} 

查看:

@model RegisterModel 

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

@using (Html.BeginForm()) 
{ 
    @Html.LabelFor(model => model.Email) 
    @Html.EditorFor(model => model.Email) 
    @Html.ValidationMessageFor(model => model.Email) 
    <input type="submit" value="OK" /> 
} 
+0

Dang,我根本找不到我的工作:(我的'ValidationMessageFor'在那裏,但它從來沒有被填充......沒有任何javascript錯誤,我可以在控制檯中看到,它只是...沒有想法(是的,如果你想知道,我的視圖中有validate.js和unobtrusive.js文件):/ – balexandre 2011-04-19 07:42:09

+0

@balexandre,你的控制器動作是怎樣的? – 2011-04-19 07:53:15

+0

剛剛添加到問題中,但工作正常,傳遞正確的值,正如你可以在FireBug控制檯中看到的一樣......我只是使用電子郵件創建了一個簡單的視圖,但我仍然沒有得到驗證消息:( – balexandre 2011-04-19 08:03:28

0

我遇到了同樣的問題,並通過更新到最新的jQuery(1.6)和jQuery.validate(1.8)庫來解決它。獲取這些最簡單的方法是在NuGet中搜索jQuery。