Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC Jasny如何在编辑操作中检查现有文件_Asp.net Mvc - Fatal编程技术网

Asp.net mvc ASP.NET MVC Jasny如何在编辑操作中检查现有文件

Asp.net mvc ASP.NET MVC Jasny如何在编辑操作中检查现有文件,asp.net-mvc,Asp.net Mvc,我正在使用添加和更新图片到文章中。当用户添加或编辑文章时,一切正常,除非用户选择编辑具有现有图像的文章,但随后不更改或删除图像 在这个实例中,我的文件输入基本上是空的,因此我的代码目前假定用户已经删除了图像,并相应地设置了model属性 在我的控制器中,我有以下功能: if (file != null) { var fileName = Guid.NewGuid().ToString() + Path.GetFileName(file.FileName); var path =

我正在使用添加和更新图片到文章中。当用户添加或编辑文章时,一切正常,除非用户选择编辑具有现有图像的文章,但随后不更改或删除图像

在这个实例中,我的文件输入基本上是空的,因此我的代码目前假定用户已经删除了图像,并相应地设置了model属性

在我的控制器中,我有以下功能:

if (file != null)
{
    var fileName = Guid.NewGuid().ToString() + Path.GetFileName(file.FileName);
    var path = Path.Combine(Server.MapPath("/Content/ArticleImages"), fileName);
    file.SaveAs(path);
    articleToUpdate.ImagePath = fileName;
}
else
{
    articleToUpdate.ImagePath = null;
}
我的看法是:

@if (Model.article.ImagePath == null)
{
    <div class="fileupload fileupload-new" data-provides="fileupload">
        <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px; margin-top: 10px">
        </div>
        <div>
            <span class="btn btn-file"><span class="fileupload-new">Select image</span><span
                class="fileupload-exists">Change</span><input type="file" name="file" /></span>
            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
        </div>
    </div>
}
else
{
    <div class="fileupload fileupload-exists" data-provides="fileupload">
        <div class="fileupload-preview thumbnail" style="width: 200px; height: 150px; margin-top: 10px">
            <img src="/Content/ArticleImages/@Model.article.ImagePath"/>
        </div>
        <div>
            <span class="btn btn-file"><span class="fileupload-new">Select image</span><span
                class="fileupload-exists">Change</span><input type="file" name="file" /></span>
            <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
        </div>
    </div>
}
@if(Model.article.ImagePath==null)
{
选择imageChange
}
其他的
{
选择imageChange
}

如何在我的控件中检查用户是否未修改图像,而不是删除或更改图像?

在视图中添加一个隐藏字段,该字段将显示viewmodel的一个属性,指示图像的存在

看法

视图模型

public bool HasImage
{
    get { return !string.IsNullOrEmpty(this.ImagePath); }
}
现在在你的控制器里

if (file != null)
{
    // Save your Image
}
else if (!model.HasImage)
{
    articleToUpdate.ImagePath = null;
}
您可能需要在视图中添加按钮/javascript,以便用户可以删除和更改隐藏字段的值
HasImage

if (file != null)
{
    // Save your Image
}
else if (!model.HasImage)
{
    articleToUpdate.ImagePath = null;
}