Jquery 文件上传,客户端脚本未调用?数据注释

Jquery 文件上传,客户端脚本未调用?数据注释,jquery,asp.net-mvc,Jquery,Asp.net Mvc,在我的MVC web应用程序中,我有一个带有imageupload组件的表单。imageupload图像数据通过数据注释进行验证。 我的服务器端验证正在工作,但没有调用我的客户端jQuery函数。。。我不知道为什么-帮助: 1。在我的视图中上载文件: @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/Scripts/jquery.validate.min.js") @Scripts.Render("~/Scripts/jquery.va

在我的MVC web应用程序中,我有一个带有imageupload组件的表单。imageupload图像数据通过数据注释进行验证。 我的服务器端验证正在工作,但没有调用我的客户端jQuery函数。。。我不知道为什么-帮助:

1。在我的视图中上载文件:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")

<input type="file" id="FileUploader" name="FileUploader" multiple />
<div id="FileDisplay" name="FileDisplay"></div>
@Html.ValidationMessageFor(model => model.FileUploader, "", new { @class = "text-danger" })
[ValidImageUpload]
public IEnumerable<HttpPostedFileBase> FileUploader { get; set; }
public sealed class ValidImageUpload : ValidationAttribute, IClientValidatable
{
    string[] stringArray = { "gif", "jpg", "png", "jpeg" };

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        IEnumerable<System.Web.HttpPostedFileBase> uploadedFiles = (IEnumerable<System.Web.HttpPostedFileBase>)value;
        var firstfile = uploadedFiles.FirstOrDefault();

        if (firstfile != null)
        {
            foreach (var file in uploadedFiles)
            {
                int pos = Array.IndexOf(stringArray, file.ContentType.Substring(6));

                if (pos > -1)
                {
                    if (file.ContentLength > 5242880)
                    {
                        return new ValidationResult(String.Format("Billedet: {0} er for stort. (Max. tilladt billede-størrelse xxxxx)", file.FileName));
                    }
                }
                else
                {
                    return new ValidationResult(String.Format("Billedt: {0} har et forkert format. Tilladte formater er - GIF, JPG, PNG, JPEG", file.FileName));
                }
            }
        }
        return ValidationResult.Success; 
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule mvr = new ModelClientValidationRule();
        mvr.ErrorMessage = "Forkert format";
        mvr.ValidationType = "validImageUpload";
        return new[] { mvr };
    }
}

您没有得到任何客户端验证的原因是您没有在html中添加适当的
dataval-*
属性。在您的情况下,始终使用强类型的
HtmlHelper
方法生成html

@Html.TextBoxFor(m => m.FileUploader, new { type="file", multiple="multiple"})
在内部,该方法从验证属性中读取metatdata,并将适当的属性添加到html中。第一次呈现页面时,
jquery.validate.unobtrusive.js
文件读取这些属性的值,并将规则添加到
jquery.valdate.js
以进行客户端验证

但是,在您的实现中还有许多其他问题。您需要验证两个独立的东西-文件大小和文件类型-因此您需要两个独立的属性。您的属性是不灵活的,因为您已将有效值硬编码到代码中(您必须重复所有代码才能将其应用到另一个只允许say
.pdf
文件的属性)。您也不会在
GetClientValidationRules()
方法中返回任何参数来指示哪些文件类型(或文件大小)是有效的。javascript函数不应该在
document.ready()中

有关
文件类型属性的实现,请参阅。有关
文件大小属性的实现,请参阅(注意,该解决方案适用于单个文件,需要修改以上载多个文件)


我也推荐您的研究。

您没有得到任何客户端验证的原因是您没有在html中添加适当的
数据val-*
属性。在您的情况下,始终使用强类型的
HtmlHelper
方法生成html

@Html.TextBoxFor(m => m.FileUploader, new { type="file", multiple="multiple"})
在内部,该方法从验证属性中读取metatdata,并将适当的属性添加到html中。第一次呈现页面时,
jquery.validate.unobtrusive.js
文件读取这些属性的值,并将规则添加到
jquery.valdate.js
以进行客户端验证

但是,在您的实现中还有许多其他问题。您需要验证两个独立的东西-文件大小和文件类型-因此您需要两个独立的属性。您的属性是不灵活的,因为您已将有效值硬编码到代码中(您必须重复所有代码才能将其应用到另一个只允许say
.pdf
文件的属性)。您也不会在
GetClientValidationRules()
方法中返回任何参数来指示哪些文件类型(或文件大小)是有效的。javascript函数不应该在
document.ready()中

有关
文件类型属性的实现,请参阅。有关
文件大小属性的实现,请参阅(注意,该解决方案适用于单个文件,需要修改以上载多个文件)


我也推荐您的研究。

您已经手动生成了您的输入,并且省略了客户端验证所需的所有
数据-*
属性。使用
@Html.TextBoxFor(m=>m.FileUploader,新的{type=“file”,multiple=“multiple”})
谢谢。如何结束此问题。您已手动生成您的输入,并省略了客户端验证所需的所有
数据-*
属性。使用
@Html.TextBoxFor(m=>m.FileUploader,新的{type=“file”,multiple=“multiple”})
谢谢。如何结束这个问题。