好的,我遇到了这样的情况:我将图像文件发布到服务器,但在此之前我验证用户是否使用“必需”属性选择该文件。还可以使用 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/