如果没有图像,请将图像源设置为null

时间:2015-12-31 09:22:43

标签: asp.net-mvc razor file-upload

我正在使用Krajee FileInput System。我有一个表单,允许用户上传照片但不是强制性的。问题是,在编辑表单时,如果没有上传文件,我会得到以下异常。

Value cannot be null or empty.
Parameter name: contentPath

如果图像为空,我如何修改接受的代码??

我试过的代码

 $(function () {
        var getCaption = function (imgUrl) {
            if (imgUrl != null) {
                var dataArray = imgUrl.split("/");
                var caption = dataArray[dataArray.length - 1];
                return caption
            }               
        }          

        //fileUpload plugin
        $("#txtPhoto").fileinput({
            showUpload: false,
            initialPreview: [
                '<img src="@Url.Content(Model.StudentRegistration.PhotoUrl!=null?Model.StudentRegistration.PhotoUrl:"")" class="file-preview-image" alt="' + getCaption("@Model.StudentRegistration.PhotoUrl") + '" title="' + getCaption("@Model.StudentRegistration.PhotoUrl") + '">'
            ],
            overwriteInitial: true,
            initialCaption: getCaption("@Model.StudentRegistration.PhotoUrl")
        });
    });

1 个答案:

答案 0 :(得分:0)

你能尝试这样吗

 initialPreview: [
              @if (Model.StudentRegistration.PhotoUrl != null)
             {
                '<img src="@Model.StudentRegistration.PhotoUrl)" class="file-preview-image" alt="' + getCaption(" @Model.StudentRegistration.PhotoUrl ") + '" title="' + getCaption(" @Model.StudentRegistration.PhotoUrl ") + '">'
             }
         ],
相关问题