2014-02-28 89 views
5

我試圖建立一個類似於在這個例子中遠程驗證: Example傳遞一個變量來驗證

我的應用程序的扭轉然而,我的表單元素是動態生成的,因此本文標籤:

[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")] 

是不是一成不變的,我需要改變例如和的ErrorMessage優選變化的動作。是否有可能,或者你會建議採取長遠的方式,這意味着我自己實現整個ajax驗證。

任何建議表示讚賞。

回答

3

如果你需要有一個動態的錯誤信息,那麼你可以從你的驗證動作返回此爲字符串:

public ActionResult DoesUserNameExist(string username) 
{ 
    if (Exists(uasername)) 
    { 
     string errorMessage = "Some dynamic error message"; 
     return Json(errorMessage, JsonRequestBehavior.AllowGet); 
    } 

    return Json(true, JsonRequestBehavior.AllowGet); 
} 

如果您需要更大的靈活性,如動態調用動態行爲,那麼你更好地滾動您的自定義驗證解決方案,而不是依賴內置的Remote屬性。

+0

對於我們的動態表單,我選擇實現自定義驗證,它似乎是最靈活的方式。 – RealityDysfunction

2

您可以繼承RemoteAttribute,並根據您自己的邏輯從服務或工廠獲取所需的值。這裏有一個例子:

[AttributeUsage(AttributeTargets.Property)] 
    public class MyRemoteAttribute : RemoteAttribute 
    { 
     public MyRemoteAttribute(Type type, string propertyName) 
      : base(MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName).Action,    MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName).Controller) 
     { 
      var data = MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName); 
      base.ErrorMessage = data.ErrorMessage; 
      base.HttpMethod = data.HttpMethod; 
     } 
    } 

    public static class MyRemoteAttributeDataProvider 
    { 
     public static RemoteAttributeData GetAttributeData(Type type 
      , string propertyName) 
     { 
      //this is where you are going to implement your logic im just implementing     as an example 
      //you can pass in a different type to get your values. For example you can pass in a service to get required values. 

      //property specific logic here, again im going to implement to make this 
      //specification by example 
      var attrData = new RemoteAttributeData();    
      if(propertyName == "MyOtherProperty") 
      { 
       attrData.Action = "MyOtherPropertyRelatedAction"; 
       attrData.Controller = "MyOtherPropertyRelatedController"; 
       attrData.ErrorMessage = "MyOtherPropertyRelated Error Message"; 
       attrData.HttpMethod = "POST"; 
      }    
      else 
      {    
       attrData.Action = "UserNameExists"; 
       attrData.Controller = "AccountController"; 
       attrData.ErrorMessage = "Some Error Message"; 
       attrData.HttpMethod = "POST"; 
      } 
      return attrData; 
     } 
    } 

    public class RemoteAttributeData 
    { 
     public string Controller { get; set; } 
     public string Action { get; set; } 
     public string HttpMethod { get; set; } 
     public string ErrorMessage { get; set; } 
    } 

這就是你應該如何使用方法是:

public class RegisterViewModel 
{ 
    [Required] 
    [Display(Name = "User name")] 
    [MyRemote(typeof(RegisterViewModel),"UserName")] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

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

    [Required] 
    [MyRemote(typeof(RegisterViewModel),"MyOtherProperty")] 
    public string MyOtherProperty { get; set; } 
} 

像我上面提到的評論。您應該根據您的需求專門提供該提供商。

我希望這會有所幫助。

更新: 我根據您的評論更新實現,以便它採用屬性名稱並執行一些屬性名稱特定的接線。

+0

但是有一個問題,如果我有幾個字段需要在頁面上檢查,我怎麼能告訴驗證者我指的是哪個字段? – RealityDysfunction

+0

@RealityDysfunction我根據你的評論更新了答案。 –

+0

這甚至更好,但在我的情況下,這個字段需要是一個變量,因爲我永遠不知道哪些元素將在頁面上。我的模型看起來像列表問題... – RealityDysfunction