仅从数据库mvc中删除图像

时间:2014-04-10 12:02:23

标签: asp.net-mvc image view edit

Oke所以我的SQL服务器数据库中有一个表格如下:

QUESTION
__________
id     | int
weight | int
title  | varchar
image  | varbinary
...

现在我创建了一个管理面板,您可以在其中编辑字段的值,以及在哪里可以选择可以上传的新图片。但在此编辑视图中,我还希望用户能够仅删除该图像。我怎么能这样做?

这是我的观点

@using (Html.BeginForm("Edit", "Question", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 @Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Edit</legend>

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

    <div class="editor-label">
        <h4>
            @Html.LabelFor(model => model.title)
        </h4>
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.title, new { cols = 50, @rows = 3 })
        @Html.ValidationMessageFor(model => model.title)
    </div>

    <div class="editor-label">
        <h4>
            @Html.LabelFor(model => model.text)
        </h4>
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.text, new { cols = 50, @rows = 3 })
        @Html.ValidationMessageFor(model => model.text)
    </div>
    <div class="editor-label">
        <h4>
            @Html.LabelFor(model => model.weight)
        </h4>
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.weight)
        @Html.ValidationMessageFor(model => model.weight)
    </div>
    <div class="editor-label">
        <h4>
            @Html.LabelFor(model => model.difficulty)
        </h4>
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.difficulty)
        @Html.ValidationMessageFor(model => model.difficulty)
    </div>
    <div class="editor-label">
        <h4>
            @Html.LabelFor(model => model.image)
        </h4>
    </div>
    <div class="editor-field">
        @Html.HiddenFor(model => model.image)
        <div class="input-group">
            <span class="input-group-btn">
                <span class="btn btn-primary btn-file">
                    Browse&hellip; <input type="file" name="imgUserProfile" id="imgUserProfile" title="Browse" onchange="PreviewImage();" />
                </span>
            </span>
            <input type="text" class="form-control" style="width: 250px" readonly>
        </div>
        <br />
        <br />
          <img id="uploadPreview" style="width: 200px; " />
         <br />

        </div>
    <br />

    <h2>Statements</h2>
    @Html.ActionLink("add statement", "Create", "Statement", new { id = Model.id }, new { @class = "btn btn-warning btn-default" })
    <br />
    <br />
    @Html.Action("_StatementPerQuestion", "Question", new { id = Model.id })
    <p>
        <input type="submit" value="Save" class=" btn btn-success" />
        @Html.ActionLink("Back to List", "Index", null, new { @class = "btn btn-info" })
    </p>
</fieldset>
}

2 个答案:

答案 0 :(得分:1)

您可以渲染图像并在用户单击它时将其删除以使其不可见。单击按钮后,将布尔值保存到控制器中的隐藏字段,以便您知道是否要删除图像。

通过视图中的id从数据库中检索数据后,您将获取填充的对象并为列基传递null。它的基数必须接受空字段

答案 1 :(得分:0)

我自己找到了一个解决方案,但它与@Daniel Melo非常相似。

我添加了一个按钮,用于传递在我的控制器中调用此函数的问题的ID

 public ActionResult DeletePic(int id = 0)
    {

        question question = db.question.Find(id);
        if (question == null)
        {
            return HttpNotFound();
        }
        else
        {
            if (ModelState.IsValid)
            {
                question.image = null;
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", "Question", new { id = question.id });
            }
        }
        return View(question);

    }