javascript - ASP.net jQuery 对话框未完成发布

标签 javascript jquery asp.net-mvc

我有一个简单的示例 View ,其中有两个(略有不同)链接,一个带有内联确认,另一个使用 JQuery 显示对话框。它们应该都出现在同一个地方,并且它们似乎都正确地命中了 Controller 事件。内联操作工作得很好,如果得到肯定的响应,我们将重定向到结果页面。 jQuery/JavaScipt 事件也会触发 Controller ,我什至将其跟踪到结果页面的渲染中,但实际上没有任何内容渲染到屏幕上。

现在,我做的唯一有点奇怪的事情是隐藏字段,用于存储结果操作的 url,但像这样存储它似乎是正确的做法,以避免 URL 被错误读取的问题通过 JavaScript。

Dialog.cshtml:

@using System.Globalization
@{
    ViewBag.Title = "Dialog";
}

<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.1.0.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.js"></script>
<script src="~/Scripts/bootstrap.js"></script>

<h2>Dialog</h2>

<p><a href='@Url.Action("Result")' onclick="return confirm('Are you really sure?');">Click Me</a></p>

<button type="button" id="TestLink">Show JQuery Dialog</button>

<input type="hidden" id="RouteUrl" value='@Url.Action("Result")' />

<div id="dialog" title="jQuery UI in ASP.NET MVC">
    <p>You now have the power of ASP.NET, the simplicity of client-side scripting with jQuery, and the looks of jQuery UI.  Congrats, web slinger!</p>
</div>

<script type="text/javascript">
    // This code runs on page load
    $(function () {

        // Attach a 'dialog' action to the dialog div above
        $('#dialog').dialog({
            autoOpen: false,
            modal: true,
            width: 450,
            buttons: {
                "Ok": function () {
                    $(this).dialog("close");
                     var url = $("#RouteUrl").attr("value");
                    $.get(url);

                    return true;
                },
                "Cancel": function () {
                    $(this).dialog("close");
                    return false;
                }
            }
        });

        // Show the dialog when the show-dialog click event fires
        $("#TestLink").button().click(function (e) {
            $('#dialog').dialog('open');
            e.preventDefault();
        });
    });
</script>

和 Controller

using System.Web.Mvc;

namespace MVCWorkbench.Controllers
{
    public class JavaScriptController : Controller
    {
        public ActionResult Dialog()
        {
            return View();
        }

        public ActionResult Result()
        {
            return View();
        }    
    }
} 

最佳答案

不知道我理解是否正确。您是否尝试在单击对话框中的“确定”按钮时浏览到该 url,或者尝试从对话框 ActionResult 加载数据并将其放入对话框中?

根据您的代码,您似乎在对话框中单击“确定”后尝试浏览该页面。如果是这样,则不要使用

$.get(url); 

使用:

window.location.href(url);  

jQuery 正在从服务器检索数据,但您当前没有对其执行任何操作。

关于javascript - ASP.net jQuery 对话框未完成发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23689755/

相关文章:

javascript - 如何捕获 Select2 下拉菜单上的滚动事件?

jQuery 模式对话框和 CKEditor 输入对话框

javascript - ReplaceAll 以清理字符串

jquery - Telerik 扩展 DatePicker 和 JQuery DatePicker

c# - DbContext 可以组合而不是继承吗?

asp.net-mvc - ASP.NET4.0 MVC4 IIS7.5 错误 403.14 我真是拔头发了

javascript - Jquery:以编程方式触发大型下拉菜单上的点击事件

javascript - 遍历子节点并查找属性(最多n层)

JavaScript 倒计时时钟

javascript - 是否有任何跨浏览器的 javascript 使 vh 和 vw 单位工作