javascript - jQuery 验证和图像文件

标签 javascript c# jquery asp.net-mvc-4 unobtrusive-validation

好的,我遇到了这样的情况:我将图像文件发布到服务器,但在此之前我验证用户是否使用“必需”属性选择该文件。还可以使用 java 函数预览图像。现在的问题是,在编辑模式下,当信息加载到页面时,我显然没有加载图像文件,而是只是创建预览路径并将其分配给图像路径。因此,当我再次编辑后保存信息时,询问我“需要图像”。系统是正确的,因为我没有选择任何图像。我该如何解决这个问题?我想强制执行 jquery 验证,但在编辑模式下,它应该跳过图像验证步骤。

脚本

<script>
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();            
        reader.onload = function (e) {
            $('#imgpreview1').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#file").change(function () {
    readURL(this);
});


<script/>                    

查看

@Html.Label("Venue Description Background") 
@Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file", id="file", required="required"})

<img src="@Model[0].Image.ImagePath" id="imgpreview1"  style="height:150px;width:150px"/>

型号

   public iSPYImage Image { get; set; }

    [DisplayName("Image File")]
    public HttpPostedFileBase ImageFile{ get; set;}

最佳答案

在 View 定义中使用某些条件。像这样:

if(editMode)
     @Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file",        id="file"})
else
     @Html.TextBoxFor(model => model[0].Image.ImageFile, new { type = "file", id="file", required="required"})

关于javascript - jQuery 验证和图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673478/

相关文章:

javascript - 当服务器和客户端在同一系统上时如何捕获JSON对象

javascript - 为什么 ('b' +'a'++ 'a' + 'a').toLowerCase() 'banana' 的结果是什么?

c# - WPF:具有重复对象引用的 SelectedItems

javascript - 访问和更改数据值

javascript - 使用数组中的参数调用函数 - apply() 而不使用上下文参数?

javascript - Bootstrap JS Scrollspy 进入 Bootstrap 面板

c# - 'x' 上的 'y' 属性无法设置为 'System.Decimal' 值。您必须将此属性设置为类型为 'System.Boolean' 的非空值

c# - 如何在 C# 中使用 EWS 托管 API 订购电子邮件

javascript - 在 Ember 中使用 AJAX 发布后没有文件下载

页面内任何表单上的 JQuery Keyup?