我使用客戶端驗證創建自己的數據註釋,以檢查所選文件是否被允許,但是我無法使其工作。客戶端方法不會被激發。上傳文件的自定義客戶端驗證
我沒有收到任何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>
爲什麼該方法不被解僱了任何想法?
您可以顯示makeFileList()JS功能,並與數據註釋模型屬性嗎? – markpsmith
@markpsmith我編輯了我的評論。 – Jamie
我看不到makeFileList()函數 – markpsmith