简体   繁体   English

如何在Javascript中定义URL来调用驻留在控制器中的ActionResult方法

[英]How to define URL in Javascript to call ActionResult methode which resides in a controller

i have created web application in MVC using Entity Framework to perform CRUD operations. 我已经使用实体框架在MVC中创建了Web应用程序来执行CRUD操作。 in when i Delete any record it takes me to Delete View and then i am able to Delete that Record. 在我删除任何记录时,需要我删除视图,然后才能删除该记录。 but instead of doing that i want to have a alert box stating "Are you sure you want to delete this record" through java script and then without talking user to another page i want to delete the record. 但是,我没有这样做,而是希望有一个警报框,它通过Java脚本声明“您确定要删除此记录吗”,然后在不与用户交谈的情况下,我想删除该记录。

i am using following option but its not helping... 我正在使用以下选项,但无济于事...

$(function () {
    $('table.dataList tbody a[linktype="Delete"]').click(function (e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/DepartmentController/DeleteConformed/',
            dataType: 'script',
            success: function () {
                alert('Are you sure you want to Delete this Department');
            },
            error: function (req, status, err) {
                alert(err);
            }
        });
    });

It is caching error stating "Not Found". 它正在缓存错误,指出“未找到”。 Am i doing any mistake while stating URL. 我在声明URL时是否犯了任何错误?

Your success callback is invoked after the request to the server has already been sent and the record already deleted. 已经发送了对服务器的请求并且记录已删除之后,将调用success回调。

You need to confirm the user intention before you're performing the Ajax request, typically using the window.confirm method. 您需要确认用户的意图,你正在执行Ajax请求之前 ,通常使用window.confirm方法。

Something like: 就像是:

$('table.dataList tbody a[linktype="Delete"]').click(function (e) {
    if (!window.confirm("Are you sure you want to delete this Department?"))
        return;

    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: '/DepartmentController/DeleteConformed/',
        dataType: 'script',
        success: function () {
            alert('Department deleted successfully');
        },
        error: function (req, status, err) {
            alert(err);
        }
    });
});

See Documentation 请参阅说明文件

Use jquery dialog. 使用jQuery对话框。 In your View add: 在您的视图中添加:

<div id="confirm-dialog"></div>

and change your function: 并更改您的功能:

$('table.dataList tbody a[linktype="Delete"]').click(function (e) {
    e.preventDefault();
    $("#confirm-dialog").dialog({
        title: "Are you sure you want to delete this Department?",
        autoOpen: false,
        modal: true,
        height: 'auto',
        close: function () {
            $(this).dialog("close");
        }
    });

    $("#confirm-dialog").dialog({
        buttons: {
            "OK": function () {
                $.ajax({
                    type: 'POST',
                    url: '/DepartmentController/DeleteConformed/',
                    dataType: 'script',
                    success: function () {
                        alert('Are you sure you want to Delete this Department');
                    },
                    error: function (req, status, err) {
                        alert(err);
                    }
                });
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
 });

Currenty what you're doing invokes the action method and only after that your confirmation dialog shows. 当前,您正在执行的操作将调用action方法,并且仅在确认对话框显示之后。 I think you need the reverse. 我认为您需要相反。 Assuming you have a button in your View that leads the user to the deletion of a certain record I would do this. 假设您的“视图”中有一个按钮可以导致用户删除某些记录,我将执行此操作。 First, I would have a function on the client side that would just ask user to confirm if he/she is sure and set the button onclick to that function. 首先,我将在客户端具有一个仅要求用户确认自己是否确定的功能,然后将onclick按钮设置为该功能。

 function confirmDelete(){
    $('<div id="dialog">').dialog({
             text:"Are you sure you want to delete?",
             modal:true,
             buttons:[{
                text: 'Yes',
                click: yesDeleteIt()
                },
             {
                text: 'No',
                click: function ()
                {
                   $(this).dialog('close');
                }
             }]
       })
 }

And in your yesDeleteIt function you can go AJAX: 在yesDeleteIt函数中,您可以使用AJAX:

  function yesDeleteIt(){
     url= '/Department/DeleteConformed/';
     $.post(url);//
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM