2013-07-11 155 views
0

在我的MVC項目我有這樣的模式:如何獲得自定義驗證器來驗證客戶端?

namespace CameraWebApp.Models 

    public class Images 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ImageId { get; set; } 
     [Required(ErrorMessage="Please enter your first name")] 
     public string SubmitterFirstName { get; set; } 
     [Required(ErrorMessage = "Please enter your surname name")] 
     public string SubmitterLastName { get; set; } 
     [ExistingFileName] 
     public string NameOfImage { get; set; } 
     [StringLength(140, ErrorMessage="Please reduce the length of your description to below 140 characters")] 
     [DataType(DataType.MultilineText)] 
     public string DescriptionOfImage { get; set; } 
     public string ImagePath { get; set; } 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public DateTime DateAdded { get; set; } 
    } 

正如你可以看到NameOfImage屬性具有屬性[ExistingFileName]這是一個自定義的驗證,該驗證程序的代碼如下:

//Overiding isValid to make a custom Validator 
protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext) 
{ 
    if (value!=null) 
    { 
     string fileName = value.ToString(); 
     if (FileExists(fileName)) 
     { 
      //If the file exists use default error message or the one passed in if there is one 
      return new ValidationResult(ExistingImageErrorMessage ?? defaultExistingImage); 
     } 
     else 
     { 
      return ValidationResult.Success; 
     } 
    } 
    else 
    { 
     //If theres no value passed in then use error message or default if none is passed 
     return new ValidationResult(ErrorMessage ?? DefaultErrorMessage); 
    } 
} 

bool FileExists(string fileName) 
{ 
    bool exists = false; 
    //A list is passed all the images 
    List<Images> images = cameraRepo.getAllImages().ToList(); 
    //Loops through every image checking if the name already exists 
    foreach (var image in images) 
    { 
     if (image.NameOfImage==fileName) 
     { 
      exists = true; 
      break; 
     } 
    } 
    return exists; 
} 

以上每個屬性均在以下代碼中驗證客戶端:

@using (Html.BeginForm()) 
{ 
<div id="errorMessages"> 
@Html.ValidationSummary(false) 
</div> 
<label>base64 image:</label> 
<input id="imageToForm" type="text" name="imgEncoded"/> 
<label>First Name</label> 
@Html.EditorFor(model => model.SubmitterFirstName) 
<label>Last Name</label> 
@Html.EditorFor(model => model.SubmitterLastName) 
<label>Name of Image</label> 
@Html.EditorFor(model => model.NameOfImage) 
<label>Image Description</label> 
@Html.EditorFor(model => model.DescriptionOfImage) 
<input type=button id="button"value=" Camera button"/> 
<input type="submit" value="Click this when your happy with your photo"/> 
} 
</div> 
@Html.ActionLink("gfd!!", "DisplayLatest") 
<script src="@Url.Content("~/Scripts/LiveVideoCapture.js")" type="text/javascript"></script> 

全部驗證工作客戶端除了我的自定義驗證[ExisitingFileName],我不知道爲什麼?有誰知道這可能是爲什麼? 在此先感謝!

回答

2

由於它是自定義驗證,因此c#mvc無法生成客戶端驗證:您必須爲此字段(使用Javascript)實現您自己的自定義客戶端驗證。在其中,您可能想要使用AJAX調用服務器方法來檢查文件名是否已存在。

您也可以嘗試使用遠程驗證,這似乎是簡單的: http://msdn.microsoft.com/en-us/library/ff398048(VS.100).aspx