发送Jquery UI对话框值到Url.Action [英] Send Jquery UI Dialog value to Url.Action

查看:43
本文介绍了发送Jquery UI对话框值到Url.Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定如何将布尔值传递给我的(有效的)C#方法DeleteTestuser.我已经对此进行了Google搜索,但是里程数因各种陷阱而异,例如,旧信息,错误的语法.

Not sure how to pass along a bool to my (working) C# method DeleteTestuser. I've Googled the heck out of this but mileage varies with all kinds of pitfalls, i.e. old information, bad syntax.

在下面,如果用户确认操作,则需要返回布尔值,而不是传递确认为false的值.谢谢...

Rather than passing confirm as false, below, I need to return a bool if the user confirms the action. Thanks...

index.cshtml

index.cshtml

<a href="@Url.Action("DeleteTestUser", "Home",
    new {id = testUser.TestUserId, confirm = false})" 
id="confirm-delete">

_layout.cshtml

_layout.cshtml

    <script type="text/javascript">
    $(function () {

        $('#dialog-modal').dialog(
            {
                title: 'Test User',
                draggable: false,
                resizeable: false,
                closeOnEscape: true,
                modal: true,
                autoOpen: false,
                buttons: {
                    'Yes': function () {
                        $(this).dialog('close');
                        confirmResult(true);

                    },
                    'No': function () {
                        $(this).dialog('close');
                        confirmResult(false);
                    }
                }
            });

        $('#confirm-delete').click(function () {
            $('#dialog-modal').dialog("open");
        });

        function confirmResult(result) { return result }
    });

</script>

推荐答案

基本上,您正在使用jQuery UI对话框重新创建自己的confirm().我做到了,这是一个类似的案例:使用jquery UI确认表单提交

Basically, you're recreating your own confirm() with jQuery UI Dialog. I did this and here is a similar case: confirm form submit with jquery UI

将此应用到您的方案中,您将得到类似的东西:

Apply this to your scenario and you have something like:

$(function() {
  function ui_confirm(message, callback) {
    var dfd = $.Deferred();
    var dialog = $("<div>", {
        id: "confirm"
      })
      .html(message)
      .appendTo($("body"))
      .data("selection", false)
      .dialog({
        autoOpen: false,
        resizable: false,
        title: 'Confirm',
        zIndex: 99999999,
        modal: true,
        buttons: [{
          text: "Yes",
          click: function() {
            $(this).dialog("close");
            dfd.resolve(true);
            if ($.isFunction(callback)) {
              callback.apply();
            }
          }
        }, {
          text: "No",
          click: function() {
            $(this).dialog("close");
            dfd.resolve(false);
          }
        }],
        close: function(event, ui) {
          $('#confirm').remove();
        }
      });
    dialog.dialog("open");
    return dfd.promise();
  }
  
  function deleteUser(id){
    // Code you will execute to delete a user or POST back.
  }
  
  $(".button").button();
  $('.del').click(function(e) {
    e.preventDefault();

    // your code

    $.when(ui_confirm("Are you sure?")).done(function(val) {
      if (val) {
        console.log("Delete User Confirmed.");
        deleteUser($(this).attr("id"));
      } else {
        console.log("Do not delete user.");
      }
    });
  });
});

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
John Smith <a href="#del-user" class="del button" id="user-0001">Delete</a>

您可能只需要执行特定的回调就可以摆脱困境.这取决于你.然后,该代码也可以用于传递其他功能或与prompt()类似的对话框一起使用.

You may be able to get away with just executing specific callbacks. That's up to you. This code can then also be used to pass along another function or to use with a prompt() like dialog.

更新

请参阅:在javascript中使用Url.Action

例如:

function deleteTestUser(uid, conf){
  var url = '@Url.Action("DeleteTestUser", "Home", new {id=' + uid + ', confirm=' + conf + '})';
  $.get(url, function(data){
    console.log("User " + uid + " Deleted.");
  });
}

如果可能,我会使用POST.

I would use POST if possible.

function deleteTestUser(uid, conf){
  $.post('@Url.Action("DeleteTestUser", "Home")', { id: uid, confirm: conf }, function(data){
    console.log("User " + uid + " Deleted.");
  });
}

这篇关于发送Jquery UI对话框值到Url.Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆