2011-04-06 161 views
0

這是我的模型:asp.net的MVC 3遠程驗證問題

[Required(ErrorMessage = "Email required!")] 
    [Remote("EmailExists","User",ErrorMessage = "Email already")] 
    public virtual string Email { get; set; } 

查看:

@Html.TextBoxFor(x => x.Email) 
    @Html.ValidationMessageFor(x => x.Email) 

控制器:

public ActionResult EmailExists(string Email) 
    { 
     return Json(!Email.Equals("[email protected]"), 
             JsonRequestBehavior.AllowGet); 
    } 

jquery.validate.min.jsjquery.validate.unobtrusive.min.js加入。並且也配置了web.config

當我輸入電子郵件輸入時,它觸發EmailExists罰款。還返回true/false。 但訥韋爾顯示的ErrorMessage

而且我得到這個錯誤:

Erro: uncaught exception: 
[Exception... "Cannot modify properties of a WrappedNative" 
nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)" 
location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: 
onxblpopuphiding :: line 848" data: no] 

任何想法?

回答

3

你的描述中沒有任何東西可以解釋問題。我創建使用默認模板創建一個新的ASP.NET MVC 3應用程序,增加了模型:

public class MyViewModel 
{ 
    [Required(ErrorMessage = "Email required!")] 
    [Remote("EmailExists", "Home", ErrorMessage = "Email already")] 
    public string Email { get; set; } 
} 

更新HomeController

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

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

    public ActionResult EmailExists(string Email) 
    { 
     return Json(
      !Email.Equals("[email protected]"), 
      JsonRequestBehavior.AllowGet 
     ); 
    } 
} 

~/Views/Home/Index.cshtml觀點:

@model AppName.Models.MyViewModel 
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.validate.min.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/scripts/jquery.validate.unobtrusive.min.js")"></script> 
@using (Html.BeginForm()) 
{ 
    @Html.LabelFor(x => x.Email) 
    @Html.TextBoxFor(x => x.Email) 
    @Html.ValidationMessageFor(x => x.Email) 
    <input type="submit" value="OK" /> 
} 

顯示驗證錯誤並顯示正確的錯誤消息(使用Chrome 10.0,IE9和FireFox 4.0進行測試)。所以現在的問題是你的情況如何不同於這個?

+1

感謝達林...我發現這個問題......這是jquery.validate版本...我使用jQuery 1.5.1和jquery.validate 1.7 ...所以我更新到1.8,一切正常... ... – Paul 2011-04-07 12:24:11

0

你只需要做到這一點:

[Required(ErrorMessage = "Email required!")] 
    [Remote("EmailExists","User")] 
    public virtual string Email { get; set; } 

public JsonResult EmailExists(string Email) 
    { 
     string errorMessage = "Email already"; 
     if (!Email.Equals("[email protected]")) 
      return Json(true, JsonRequestBehavior.AllowGet); 

     return Json(errorMessage, JsonRequestBehavior.AllowGet); 
    }