0

Jasny Fileuploadを使用して、画像を記事に追加および更新しています。ユーザーが記事を追加または編集する場合、ユーザーが既存の画像を使用して記事を編集することを選択したものの、画像を変更または削除しない場合を除いて、すべて正常に機能します。

基本的に、この場合、ファイル入力は null であるため、現時点でのコードは、ユーザーが画像を削除したと想定し、それに応じてモデル プロパティを設定します。

私のコントローラーには次のものがあります。

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>
}

コントローラーで、ユーザーが画像を削除または変更するのではなく、画像を変更していないかどうかを確認するにはどうすればよいですか?

4

1 に答える 1

1

画像の存在を示すビューモデルのプロパティを公開する隠しフィールドをビューに追加します。

意見

@Html.HiddenFor(model => model.HasImage)

ビューモデル

public bool HasImage
{
    get { return !string.IsNullOrEmpty(this.ImagePath); }
}

今あなたのコントローラーで

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

ユーザーが非表示フィールドの値を削除および変更できるように、ビューにボタン/JavaScript を追加する必要がある場合がありますHasImage

于 2013-07-15T23:03:24.110 に答える