Вопрос

I've tried with JavaScript in so many ways but I can't figure this one out, I really don't know what to do here. I just want a confirm box for this control/action.

@using (Html.BeginForm("Del_img", "Home", new { Name = @item.Url }))
{
    <input type="submit" value="Delete" />
}

Regards

Это было полезно?

Решение

If you want to send form, you can use:

@using (Html.BeginForm("Del_img", "Home", new { Name = @item.Url, @id="myForm" }))
{
    <input type="submit" value="Delete" onclick ="ConfirmDelete()" />
}

<script>
    function ConfirmDelete() {
        if (confirm("Are you sure to send delete form?")) {
            document.getElementById("myForm").submit();
        }
    }
</script>

Or, generally, if you want to send any parameter to delete action:

<img src="..." alt="image" onclick ="ConfirmDelete(@Model.Id)"   />

This will send id to action for deleting:

<script>
    function ConfirmDelete(id) {
        if (confirm("Are you sure to delete this?")) {
            document.location.href = '@Url.Action("Delete", "Controller")/' + id;
        }
    }
</script>

And action:

public ActionResult Delete(int id)
{
     //delete object by id
     return RedirectToAction("Index");
}

Другие советы

Add button with id or name attributes in markup(.cshtml)

In Javascript or jquery you can add the confirmation dialog box

<input type="submit" name="delete" value="Delete" /> 
<script type="text/javascript">
$(document).ready(function(){
    $("input[name='delete']").click(function() {
        return confirm('Are you sure you want to Delete?');
    });
});

Thank you for your response. Right after I asked a the question I found the solution, similar to what Jhoon Bey showed.

But I need all your responses further on for the project I'm working on. Thank you all for contributing.

Regards

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top