2014-03-28 62 views
2

我使用客戶端驗證創建自己的數據註釋,以檢查所選文件是否被允許,但是我無法使其工作。客戶端方法不會被激發。上傳文件的自定義客戶端驗證

我沒有收到任何JavaScript錯誤。

型號:

public class FotoAlbumModel 
{ 
    public int AlbumId { get; set; } 

    [Filters.Required] 
    [MaxLength(150, ErrorMessage = "Dit veld mag niet langer zijn dan 150 tekens.")] 
    public string Titel { get; set; } 

    [Filters.Required] 
    [MaxLength(150, ErrorMessage = "Dit veld mag niet langer zijn dan 2500 tekens.")] 
    public string Descriptie { get; set; } 

    [Filters.Required] 
    [MaxLength(250, ErrorMessage = "Dit veld mag niet langer zijn dan 250 tekens.")] 
    public string Keywoorden { get; set; } 

    [Filters.Required] 
    [LinkName] 
    public string Linknaam { get; set; } 

    public bool Status { get; set; } 

    public int AantalFotos { get; set; } 

    [Filters.FileExtensions(FileExtensions = ".bmp,.jpg,.png.gif,.jpeg")] 
    public HttpPostedFileBase[] Fotos { get; set; } 
} 

我的註解:

public class FileExtensionsAttribute : ValidationAttribute, IClientValidatable 
{ 
    public string FileExtensions { get; set; } 

    public override bool IsValid(object value) 
    { 
     string strValue = value == null ? "" : value.ToString(); 

     string[] arrayFileExtensions = FileExtensions.Split(','); 

     bool isMatch = arrayFileExtensions.Any(x => x.Contains(Path.GetExtension(strValue))); 

     if (!isMatch && ErrorMessage == null) 
     { 
      ErrorMessage = "De extensie van de bestand is niet toegestaan."; 
     } 
     return isMatch; 
    } 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     var rule = new ModelClientValidationRule 
     { 
      ErrorMessage = "De extensie van de bestand is niet toegestaan.", 
      ValidationType = "fileextension" 

     }; 
     rule.ValidationParameters.Add("allowedextensions", FileExtensions); 

     yield return rule; 
    } 
} 

我的自定義驗證適配器:

$.validator.unobtrusive.adapters.addSingleVal("fileextension", "allowedextensions"); 

$.validator.addMethod("fileextension", function (value, element, allowedextensions) { 
alert('sds'); 
var arrayAllowedExtensions = allowedextensions.split(','); 
var fileExtension = value.split('.').pop(); 
$.each(arrayAllowedExtensions, function(extension) { 
    if (extension == fileExtension) { 
     return true; 
    } 
}); 
return false; 
}); 

HTML:

@Html.TextBoxFor(m=> m.Fotos, new {@type="file", @multiple="true", @onchange="makeFileList();", @id="filesToUpload", @style="display: none;"}) 
           <input type="button" value="Plaatje(s) selecteren" class="btn btn-green btn-gradient" onclick="document.getElementById('filesToUpload').click();" /> 
          <ul id="fileList"> 
           <li>No Files Selected</li> 
          </ul> 
          @Html.ValidationMessageFor(m => m.Fotos) 

MakeFileList JavaScript函數:

<script> 
    function makeFileList() { 
     var input = document.getElementById("filesToUpload"); 
     var ul = document.getElementById("fileList"); 
     while (ul.hasChildNodes()) { 
      ul.removeChild(ul.firstChild); 
     } 
     for (var i = 0; i < input.files.length; i++) { 
      var li = document.createElement("li"); 
      li.innerHTML = input.files[i].name; 
      ul.appendChild(li); 
     } 
     if(!ul.hasChildNodes()) { 
      var li = document.createElement("li"); 
      li.innerHTML = 'No Files Selected'; 
      ul.appendChild(li); 
     } 
    } 
</script> 

爲什麼該方法不被解僱了任何想法?

+0

您可以顯示makeFileList()JS功能,並與數據註釋模型屬性嗎? – markpsmith

+0

@markpsmith我編輯了我的評論。 – Jamie

+0

我看不到makeFileList()函數 – markpsmith

回答

1

我想這是因爲你隱藏了FileExtensionsAttribute代碼試圖驗證的控件。

看看使用Firebug你的頁面,你可以看到隱藏的控制<input id="filesToUpload" type="file"有驗證data-val-fileextension="De extensie van de bestand is niet toegestaan."

如果您通過刪除@style="display: none;"屬性修改文本@Html.TextBoxFor(m=> m.Fotos,你會看到你的驗證工作。

不幸的是我不知道如何解決這個問題。有沒有理由不能使用標準的文件上傳控制?

+0

您是否嘗試了標準文件上傳按鈕? [http://stackoverflow.com/questions/14401550/adding-style-to-file-upload-button-in-css](http://stackoverflow.com/questions/14401550/adding-style-to-file-上傳按鈕功能於CSS) – markpsmith

0

變化:@onchange="makeFileList();"@Html.TextBoxFor(,, 到:@onchange="makeFileList()"(末尾刪除分號)

相關問題