问题描述:

on checkbox click,i want to go to partial view.

but the page is rediredted to another page intead of loading popup

 @{

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>

网友答案:

Setting window.location changes the URL in the location bar of your browser, which in turn cause the entire page view to change to the response from that URL.

If you just want to fetch a partial view, you need to do an AJAX request and then manually insert the response into the DOM in your callback:

<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>
相关阅读:
Top