2011-12-01 116 views
1

我創建了一個非常簡單的測試項目來說明問題。MVC3遠程驗證沒有被觸發?

我的模型類

public class HomeModel 
{ 
    [Required(ErrorMessage="Missing property1.")] 
    public string Property1 
    { 
     get; 
     set; 
    } 

    [Remote("ValidateProperty2", "Home", HttpMethod="Get", AdditionalFields = "Property3", ErrorMessage="Property2 wrong!")] 
    public string Property2 
    { 
     get; 
     set; 
    } 

    public string Property3 
    { 
     get; 
     set; 
    } 
} 

我控制器

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     HomeModel model = new HomeModel(); 
     return View(model); 
    } 

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

    public ActionResult ValidateProperty2(string property2, string property3) 
    { 
     return Json(true, JsonRequestBehavior.AllowGet); 
    } 
} 

而且我認爲

@model RemoteValidationTest.Models.HomeModel 

@{ 
Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
<title>Index</title> 
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<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> 
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
</head> 
<body> 
@using (Ajax.BeginForm(new AjaxOptions 
{ 
    HttpMethod = "POST" 
})) 
{ 
    @Html.TextBoxFor(x => x.Property1) @Html.ValidationMessageFor(x => x.Property1)<br /> 
    @Html.TextBoxFor(x => x.Property2) @Html.ValidationMessageFor(x => x.Property2)<br /> 
    @Html.TextBoxFor(x => x.Property3) @Html.ValidationMessageFor(x => x.Property3)<br /> 

    <input type="submit" /> 
} 
</body> 
</html> 

而且我的web.config

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

這裏沒有什麼特別的感覺。我有一個有3個屬性的模型類。第一個是需要的,第二個是遠程驗證,我相信我已經正確創建了操作方法。我將break break設置爲ValidateProperty2函數,它永遠不會被調用。

我也用螢火,同樣的事情,客戶端甚至沒有嘗試調用服務器端。

什麼是錯這裏的代碼?

編輯1: 我想我會得到一些東西,遠程驗證只會在控件(例如文本框)有值時觸發。空的控件永遠不會觸發驗證。在我的情況下,我實際上試圖暗示更復雜的邏輯,即使控件文本爲空(檢查其他屬性的值),我也需要驗證才能觸發。它甚至有可能嗎?

+0

你確認此作品,未經AdditionalFields屬性?看看這個:http://stackoverflow.com/questions/4752877/remote-validation-in-asp-net-mvc-3-how-to-use-additionalfields-in-action-method –

+0

@Mihalis Bagos,沒有AdditionalFields它不會觸發。 – hardywang

+0

由於代碼看起來不錯,然後,考慮通過的NuGet更新JS庫,然後再試一次。此外,您還可以在這裏找到工作的例子:http://msdn.microsoft.com/en-us/library/gg508808%28v=vs.98%29.aspx –

回答

0

我在這裏有一個工作版本MVC3: http://completedevelopment.blogspot.com/2011/08/remote-ajax-validation-in-mvc3.html

d /升它,你可以比較文件。

+0

我覺得我打破了你的代碼,下載的項目只是正常工作。我添加了[Remote(「AddressValidate」,「Demo」,ErrorMessage =「Address is invalid。」)] public string Address {get;組; }到您的Person類,從您的EmailAddress屬性中刪除了RegularExpression和Required。現在我的地址沒有被驗證。 – hardywang