Passing a filename to a view
<p>I am trying to build a simple CMS which allows the author to upload files (specifically images but the file type is not really important for now).</p>

<p>The upload of the file is working fine. However I want to provide the ability to list and subsequently delete a file (maybe later multiple files but for now a single file at a time is fine).</p>

<p>I have looked around the net. I see plenty of examples using EF to store the location of the file in a DB because they have permissions and roles etc. While that is something I may need way off in the future, its not a layer of complexity I am willing to add right now.</p>

<p>All I want is to simply press a delete link (just as though you are deleting a record in a DB). To trigger an action which calls a delete confirmation view. Then on that view, a delete button to actually delete the file and return the user to the list. Below is my code so far:</p>

<p>This would be the view that lists the files:</p>

<pre><code>@model IEnumerable&lt;FileInfo&gt;

@{
    ViewBag.Title = "File List";
}

&lt;h2&gt;Index&lt;/h2&gt;

&lt;p&gt;
    @Html.ActionLink("Upload", "Upload")
&lt;/p&gt;
&lt;table class="table"&gt;
    &lt;tr&gt;
        &lt;th&gt;File Name&lt;/th&gt;
        &lt;th&gt;Actions&lt;/th&gt;
    &lt;/tr&gt;

    @foreach (FileInfo file in Model)
    {
        &lt;tr&gt;
            &lt;td&gt;@file.Name&lt;/td&gt;
            &lt;td&gt;@Html.ActionLink("Delete", "Delete", new { fileName = @file.Name })&lt;/td&gt;
        &lt;/tr&gt;
    }
&lt;/table&gt;
</code></pre>

<p>I wont show the controller for this view as it's relatively simple and not where I am having the problem (I think). I only showed this so you could see the delete link and tell me if there is anything wrong.</p>

<p>Below is the delete confirmation view:</p>

<pre><code>@model FileInfo

@{
    ViewBag.Title = "Delete";
}

&lt;h2&gt;Delete&lt;/h2&gt;

&lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt;

&lt;dl class="dl-horizontal"&gt;
    &lt;dt&gt;
        @Html.DisplayNameFor(model =&gt; model.FullName)
    &lt;/dt&gt;

    &lt;dd&gt;
        @Html.DisplayFor(model =&gt; model.FullName)
    &lt;/dd&gt;
&lt;/dl&gt;
@using (Html.BeginForm("Delete", "FileManagement", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    &lt;div class="form-actions no-color"&gt;
        @Html.ActionLink("Back to list of views", "Index", null, new { @class = "btn btn-success" })
        &amp;nbsp;|&amp;nbsp;
        @*@Html.ActionLink("Delete", "Delete", null, new { @class = "btn btn-danger" })*@
        &lt;input type="submit" value="Delete file" formaction="Delete" formmethod="delete" class="btn btn-danger" /&gt;
    &lt;/div&gt;
}
</code></pre>

<p>Below are the two Delete actions (GET and POST / DELETE)</p>

<pre><code>    // GET: FileManagement/Delete/filename
    public ActionResult Delete()
    {
        return View();
    }

    // POST: FileManagement/Delete/filename
    [HttpDelete]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(string fileName)
    {
        var path = Path.Combine(Server.MapPath("~/UserFiles"), fileName);

        if (System.IO.File.Exists(path))
            System.IO.File.Delete(path);
        else
            return HttpNotFound();

        return RedirectToAction("Index");
    }
</code></pre>

<p>I don't have view models as I am not connecting to a database (yet). The files are just uploaded to the folder ~/UserFiles/someFileName.ext and the full path is got through appending this to the server.mappath in the normal way.</p>

<p>The problem I am having is getting the file name into the delete confirmation view, and also into the delete button which would pass it to the delete action to do the job.</p>

<p>Thanks for any help.</p>