模态弹出窗口不能处理url操作

时间:2016-02-24 15:21:36

标签: jquery asp.net asp.net-mvc

单击复选框上的

,我想转到局部视图。 但该页面被重新发送到另一个加载弹出窗口的页面

    @{
        var url = Url.Action("Delete", "Resume", new {data_modal = "", id = item.resumeID }, null);
    }

   <script src="~/Scripts/modalform.js"></script> 

    @Html.CheckBox("mycheckbox", new { onclick = "window.location = '" + url + "'" })

<div id='myModal' class='modal fade in'> 
    <div class="modal-dialog"> 
        <div class="modal-content"> 
            <div id='myModalContent'></div> 
        </div> 
    </div> 
</div>

1 个答案:

答案 0 :(得分:1)

设置window.location会更改浏览器位置栏中的网址,从而导致整个网页视图更改为该网址的响应。

如果您只想获取部分视图,则需要执行AJAX请求,然后在回调中手动将响应插入DOM:

<script>
    // use a JavaScript namespace to keep your global variables separate from that
    // of other scripts
    var MyNamespace = MyNamespace || {}; 

    // Here, we're setting a JavaScript variable with the URL. You can't set
    // a Razor variable and use that later in your JavaScript, because Razor
    // is server-side only, while JavaScript is client-side only.
    var MyNamespace.PopupUrl = '@Url.Action("Delete", "Resume", new {data_modal = "", id = item.resumeID }, null)';

    // Always use unobtrusive event handlers rather than HTML attributes like `onclick`
    $('#mycheckbox').on('click', function () {
        // now call AJAX to get response of URL
        $.get(MyNamespace.PopupUrl, function (html) {
            // this anonymous function is your callback
            // add the returned HTML to the DOM
            $('#myModalContent').html(html);

            // you probably want to have the modal load after this
            $('#myModal').modal('show');
        });
    });
</script>