2

我有一個Model其中有電話號碼和電子郵件MVC 4。這就像註冊。因此,用戶可以提供電子郵件或手機號碼,或者兩者都提供。但是我不能讓他們選擇一個必需的領域。 我的模型就像如何在MVC 4應用程序中創建替代必填字段?

public class RegisterModel 
    { 
     [Required] 
     [Display(Name = "User name")]   
     [UsernameValidator(ErrorMessage = "Only Letters and numbers")] 
     [System.Web.Mvc.Remote("IsUserExitst", "Account", HttpMethod = "POST", ErrorMessage = "This user name already exists. Try a new one.")] 
     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] 
     [MaxLength(50)] 
     [EmailOrPhoneValidator(ErrorMessage = "Invalid Phone number")] 
     [Display(Name = "Phone number")] 
     [System.Web.Mvc.Remote("IsMobOrEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists. Try a new one.")] 
     public string MobileNo { get; set; } 


     [Required] 
     [MaxLength(50)] 
     [EmailAddress(ErrorMessage = "Invalid Email address")] 
     [Display(Name = "Email")] 
     [System.Web.Mvc.Remote("IsEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Email already exists. Try a new one.")] 
     public string Email { get; set; } 
    } 

在這裏,我想用EmailPhoneNo交替。我想寫一個作爲自定義必需字段的類。我已經通過這個問題,

  1. Question這裏的答案提供了一個解決方案與JavaScript結合。但我不會使用它。我將在這裏寫下類文件並實現我的邏輯。
  2. RequiredIf似乎是我的問題的確切建議,但我很累,嘗試該解決方案。我無法理解如何在我的項目中導入.dll。它是在Mvc 3中開發的
  3. 另一個solution,但是在這裏他們將一個類中的可選屬性結合起來。所以我該怎麼做,因爲我的房產是完全分開的。那麼該怎麼辦。
+1

第一個鏈接有什麼問題 - 這正是您所期望的。如果沒有提供「電子郵件」,則需要提供「MobileNo」,如果未提供「MobileNo」,則需要提供「電子郵件」。我假設在第二個選項中,你提到[萬無一失](http://foolproof.codeplex.com/)屬性? (它也有'[RequiredIfNot]') –

+0

我不喜歡'javascript',因爲我不能顯示錯誤消息類似的與其他需要的錯誤。 Javascript以自己的方式顯示錯誤。 – gdmanandamohon

+1

當然可以。這正是客戶端驗證如何使用'jquery.validate.js'和'jquery.validate.unobtrusive.js'工作的方式。 –

回答

1

我會與解決方案2並創建一個RequiredIf驗證屬性。無需導入任何DLL,因爲您可以自己創建類。以下是該屬性可能的外觀示例。這是從我在我自己的代碼中使用RequiredIfTrue屬性和我交換的驗證邏輯來​​尋找空值

public class RequiredIfEmpty : ValidationAttribute 
{ 
    private string _fieldName; 

    public RequiredIfEmpty(string fieldName) 
    { 
     _fieldName = fieldName; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     var property = validationContext.ObjectType.GetProperty(_fieldName); 
     if (property == null) 
     { 
      throw new ArgumentException("No property with the name " + _fieldName + " found."); 
     } 

     string fieldValue = (string)property.GetValue(validationContext.ObjectInstance, null); 
     if (String.IsNullOrWhiteSpace(fieldValue)) 
     { 
      return (value != null && !String.IsNullOrWhiteSpace((string)value) ? ValidationResult.Success : new ValidationResult(ErrorMessage)); 
     } 
     else 
     { 
      return ValidationResult.Success; 
     } 
    } 
} 

在您的視圖模型,你可以使用像任何其他屬性的屬性,但你必須指定的名稱替代財產

[RequiredIfEmpty("Email", ErrorMessage = "Phone number invalid")] 
[MaxLength(50)] 
[Display(Name = "Phone number")] 
[System.Web.Mvc.Remote("IsMobOrEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists. Try a new one.")] 
public string MobileNo { get; set; } 
+0

您的代碼部分工作得很好。但是,如果我在驗證摘要中的兩個實體之上寫入'RequiredIfEmpty',它會顯示一條消息兩次。另一方面,如果我寫上面的任何一個,它不會標記單個輸入字段,而不是突出顯示兩個輸入框。 – gdmanandamohon

+2

這不會給任何客戶端驗證。 –

+0

是的!正是@Stephen Muecke,表單提交之後會返回驗證錯誤。 – gdmanandamohon