0

I'm not well versed in this framework so I need some help here. In a view I want to add a link or a button on clicking which a certain file gets deleted from the server. I've added this method to the controller:

    [Authorize]
    public ActionResult DeleteFile(string path)
    {
        if ((System.IO.File.Exists(path)))
        {
            try
            {
                System.IO.File.Delete(path);
            }catch(Exception ex)
            {
                Debug.WriteLine("Deletion of file failed: " + ex.Message);
            }
        }
        return View();
    }

Seemed straightforward, though I'm not sure about the return View();. Now in the view, I need a form, because the path to the file that should be deleted needs to be posted to the controller, is that correct? This is what I got so far, mimicked from other code in the project:

@Html.BeginForm("DeleteFile", "Home", FormMethod.Post, new { id = "delete-attachment-form" })
    {

        @Html.Hidden("path", path)
    }

path is a JavaScript variable containing the server path to the file that needs to be deleted. If I'm on the right track here, how do I add a button or a link to click on that will send the form?

6
  • Should just be able to add a submit button: <input type="submit" name="submit" /> Commented Sep 13, 2016 at 17:30
  • 1
    Add a submit button to your form and be sure to mark your action with [HttpPost]. Commented Sep 13, 2016 at 17:41
  • 1
    How are you using the path javascript variable exactly? The server will render your HTML from @Html.Hidden before javascript executes. Commented Sep 13, 2016 at 17:41
  • @stephen.vakil good point, I misspoke though, it's actually a C# variable inside the razor block. Commented Sep 14, 2016 at 9:11
  • @Ksib if this were an answer I'd accept it. Simple and it works. Commented Sep 14, 2016 at 10:03

3 Answers 3

1

Should just be able to add a submit button:

<input type="submit" name="submit" />

Sign up to request clarification or add additional context in comments.

Comments

1

You should have a form and a button like this

@Html.BeginForm("Controller", "DeleteFile", new {Path= filePath},FormMethod.Post) { //Button }

Or using Ajax and Jquery

var values = $(this).serialize();

$.ajax({
      type: 'POST',
      url: "url?path="+path.tostring(),
      data: values ,
      success: function(response) { //update view }
});

Comments

1

Inside your form you can add a button and then handle the button click in JavaScript.

@Html.BeginForm("DeleteFile", "Home", FormMethod.Post, new { id = "delete-attachment-form" })
{
    @Html.Hidden("path", path)
    <button id="delete-btn" type="button" class="btn btn-danger">
        Delete
    </button>
}

Then the <script type="text/javascript"> block:

$(function () {
    $('#delete-btn').click(function () {
        var query = $('#delete-attachment-form');
        var form = query[0];
        var toPost = query.serialize();
        $.ajax({
            url: form.action,
            type: form.method,
            data: toPost,
            success: function (result) {
                // display result
            },
            error: function () {
                // handle error
            }
        })            
    });
});

Also, this is a good tutorial on deleting in ASP.NET MVC

2 Comments

Thanks. But is there a reason that adding that chunk of jQuery code instead of just the line <input type="submit" name="submit" /> as proposed in the comments? Seems much simpler
Gives you access to Ajax success/error functions so you can adjust the UI dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.